go.fuchsia.dev/jiri@v0.0.0-20240502161911-b66513b29486/cmd/jiri/project_config.go (about)

     1  // Copyright 2017 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  
    11  	"go.fuchsia.dev/jiri"
    12  	"go.fuchsia.dev/jiri/cmdline"
    13  	"go.fuchsia.dev/jiri/project"
    14  )
    15  
    16  var cmdProjectConfig = &cmdline.Command{
    17  	Runner: jiri.RunnerFunc(runProjectConfig),
    18  	Name:   "project-config",
    19  	Short:  "Prints/sets project's local config",
    20  	Long: `
    21  Prints/Manages local project config. This command should be run from inside a
    22  project. It will print config if no flags are provided otherwise set it.`,
    23  }
    24  
    25  var (
    26  	configIgnoreFlag   string
    27  	configNoUpdateFlag string
    28  	configNoRebaseFlag string
    29  )
    30  
    31  func init() {
    32  	cmdProjectConfig.Flags.StringVar(&configIgnoreFlag, "ignore", "", `This can be true or false. If set to true project would be completely ignored while updating`)
    33  	cmdProjectConfig.Flags.StringVar(&configNoUpdateFlag, "no-update", "", `This can be true or false. If set to true project won't be updated`)
    34  	cmdProjectConfig.Flags.StringVar(&configNoRebaseFlag, "no-rebase", "", `This can be true or false. If set to true local branch won't be rebased or merged.`)
    35  }
    36  
    37  func runProjectConfig(jirix *jiri.X, args []string) error {
    38  	p, err := currentProject(jirix)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	if configIgnoreFlag == "" && configNoUpdateFlag == "" && configNoRebaseFlag == "" {
    43  		displayConfig(p.LocalConfig)
    44  		return nil
    45  	}
    46  	lc := p.LocalConfig
    47  	if err := setBoolVar(configIgnoreFlag, &lc.Ignore, "ignore"); err != nil {
    48  		return err
    49  	}
    50  	if err := setBoolVar(configNoUpdateFlag, &lc.NoUpdate, "no-update"); err != nil {
    51  		return err
    52  	}
    53  	if err := setBoolVar(configNoRebaseFlag, &lc.NoRebase, "no-rebase"); err != nil {
    54  		return err
    55  	}
    56  	return project.WriteLocalConfig(jirix, p, lc)
    57  }
    58  
    59  func setBoolVar(value string, b *bool, flagName string) error {
    60  	if value == "" {
    61  		return nil
    62  	}
    63  	if val, err := strconv.ParseBool(value); err != nil {
    64  		return fmt.Errorf("%s flag should be true or false", flagName)
    65  	} else {
    66  		*b = val
    67  	}
    68  	return nil
    69  }
    70  
    71  func displayConfig(lc project.LocalConfig) {
    72  	fmt.Printf("Config:\n")
    73  	fmt.Printf("ignore: %t\n", lc.Ignore)
    74  	fmt.Printf("no-update: %t\n", lc.NoUpdate)
    75  	fmt.Printf("no-rebase: %t\n", lc.NoRebase)
    76  }