|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/spf13/cobra" |
| 6 | +) |
| 7 | + |
| 8 | +func init() { |
| 9 | + RootCmd.AddCommand(configCmd) |
| 10 | + configCmd.AddCommand(configGetCmd) |
| 11 | + configCmd.AddCommand(configSetCmd) |
| 12 | + |
| 13 | + configGetCmd.Flags().StringP("host", "h", "", "Get per-host setting") |
| 14 | + configSetCmd.Flags().StringP("host", "h", "", "Set per-host setting") |
| 15 | + |
| 16 | + // TODO reveal and add usage once we properly support multiple hosts |
| 17 | + _ = configGetCmd.Flags().MarkHidden("host") |
| 18 | + // TODO reveal and add usage once we properly support multiple hosts |
| 19 | + _ = configSetCmd.Flags().MarkHidden("host") |
| 20 | +} |
| 21 | + |
| 22 | +var configCmd = &cobra.Command{ |
| 23 | + Use: "config", |
| 24 | + Short: "Set and get gh settings", |
| 25 | + Long: `Get and set key/value strings. |
| 26 | +
|
| 27 | +Current respected settings: |
| 28 | +- git_protocol: https or ssh. Default is https. |
| 29 | +- editor: if unset, defaults to environment variables. |
| 30 | +`, |
| 31 | +} |
| 32 | + |
| 33 | +var configGetCmd = &cobra.Command{ |
| 34 | + Use: "get <key>", |
| 35 | + Short: "Prints the value of a given configuration key.", |
| 36 | + Long: `Get the value for a given configuration key. |
| 37 | +
|
| 38 | +Examples: |
| 39 | + $ gh config get git_protocol |
| 40 | + https |
| 41 | +`, |
| 42 | + Args: cobra.ExactArgs(1), |
| 43 | + RunE: configGet, |
| 44 | +} |
| 45 | + |
| 46 | +var configSetCmd = &cobra.Command{ |
| 47 | + Use: "set <key> <value>", |
| 48 | + Short: "Updates configuration with the value of a given key.", |
| 49 | + Long: `Update the configuration by setting a key to a value. |
| 50 | +
|
| 51 | +Examples: |
| 52 | + $ gh config set editor vim |
| 53 | +`, |
| 54 | + Args: cobra.ExactArgs(2), |
| 55 | + RunE: configSet, |
| 56 | +} |
| 57 | + |
| 58 | +func configGet(cmd *cobra.Command, args []string) error { |
| 59 | + key := args[0] |
| 60 | + hostname, err := cmd.Flags().GetString("host") |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + ctx := contextForCommand(cmd) |
| 66 | + |
| 67 | + cfg, err := ctx.Config() |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + val, err := cfg.Get(hostname, key) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if val != "" { |
| 78 | + out := colorableOut(cmd) |
| 79 | + fmt.Fprintf(out, "%s\n", val) |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func configSet(cmd *cobra.Command, args []string) error { |
| 86 | + key := args[0] |
| 87 | + value := args[1] |
| 88 | + |
| 89 | + hostname, err := cmd.Flags().GetString("host") |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + ctx := contextForCommand(cmd) |
| 95 | + |
| 96 | + cfg, err := ctx.Config() |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + err = cfg.Set(hostname, key, value) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("failed to set %q to %q: %w", key, value, err) |
| 104 | + } |
| 105 | + |
| 106 | + err = cfg.Write() |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("failed to write config to disk: %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
| 112 | +} |
0 commit comments