github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/config/set/set.go (about) 1 package set 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "github.com/MakeNowJust/heredoc" 9 "github.com/cli/cli/internal/config" 10 "github.com/cli/cli/pkg/cmdutil" 11 "github.com/cli/cli/pkg/iostreams" 12 "github.com/spf13/cobra" 13 ) 14 15 type SetOptions struct { 16 IO *iostreams.IOStreams 17 Config config.Config 18 19 Key string 20 Value string 21 Hostname string 22 } 23 24 func NewCmdConfigSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command { 25 opts := &SetOptions{ 26 IO: f.IOStreams, 27 } 28 29 cmd := &cobra.Command{ 30 Use: "set <key> <value>", 31 Short: "Update configuration with a value for the given key", 32 Example: heredoc.Doc(` 33 $ gh config set editor vim 34 $ gh config set editor "code --wait" 35 $ gh config set git_protocol ssh --host github.com 36 $ gh config set prompt disabled 37 `), 38 Args: cobra.ExactArgs(2), 39 RunE: func(cmd *cobra.Command, args []string) error { 40 config, err := f.Config() 41 if err != nil { 42 return err 43 } 44 opts.Config = config 45 opts.Key = args[0] 46 opts.Value = args[1] 47 48 if runF != nil { 49 return runF(opts) 50 } 51 52 return setRun(opts) 53 }, 54 } 55 56 cmd.Flags().StringVarP(&opts.Hostname, "host", "h", "", "Set per-host setting") 57 58 return cmd 59 } 60 61 func setRun(opts *SetOptions) error { 62 err := config.ValidateKey(opts.Key) 63 if err != nil { 64 warningIcon := opts.IO.ColorScheme().WarningIcon() 65 fmt.Fprintf(opts.IO.ErrOut, "%s warning: '%s' is not a known configuration key\n", warningIcon, opts.Key) 66 } 67 68 err = config.ValidateValue(opts.Key, opts.Value) 69 if err != nil { 70 var invalidValue *config.InvalidValueError 71 if errors.As(err, &invalidValue) { 72 var values []string 73 for _, v := range invalidValue.ValidValues { 74 values = append(values, fmt.Sprintf("'%s'", v)) 75 } 76 return fmt.Errorf("failed to set %q to %q: valid values are %v", opts.Key, opts.Value, strings.Join(values, ", ")) 77 } 78 } 79 80 err = opts.Config.Set(opts.Hostname, opts.Key, opts.Value) 81 if err != nil { 82 return fmt.Errorf("failed to set %q to %q: %w", opts.Key, opts.Value, err) 83 } 84 85 err = opts.Config.Write() 86 if err != nil { 87 return fmt.Errorf("failed to write config to disk: %w", err) 88 } 89 return nil 90 }