|
| 1 | +// Package config provides commands for managing CLI configuration |
| 2 | +package config |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/buildkite/cli/v3/internal/config" |
| 10 | +) |
| 11 | + |
| 12 | +// ConfigCmd is the root command for managing CLI configuration |
| 13 | +type ConfigCmd struct { |
| 14 | + List ListCmd `cmd:"" help:"List configuration values." aliases:"ls"` |
| 15 | + Get GetCmd `cmd:"" help:"Get a configuration value."` |
| 16 | + Set SetCmd `cmd:"" help:"Set a configuration value."` |
| 17 | + Unset UnsetCmd `cmd:"" help:"Remove a configuration value."` |
| 18 | +} |
| 19 | + |
| 20 | +func (c ConfigCmd) Help() string { |
| 21 | + return `Manage CLI configuration settings. |
| 22 | +
|
| 23 | +Configuration is stored in two locations: |
| 24 | + User config: ~/.config/bk.yaml (global defaults) |
| 25 | + Local config: .bk.yaml (repo-specific overrides) |
| 26 | +
|
| 27 | +Precedence: Environment variable > Local config > User config > Default |
| 28 | +
|
| 29 | +Examples: |
| 30 | + $ bk config list # Show all config values |
| 31 | + $ bk config get output_format # Get a specific value |
| 32 | + $ bk config set output_format yaml # Set default output to YAML |
| 33 | + $ bk config set no_pager true --local # Disable pager for this repo |
| 34 | + $ bk config unset pager # Reset pager to default` |
| 35 | +} |
| 36 | + |
| 37 | +// ConfigKey represents a valid configuration key |
| 38 | +type ConfigKey string |
| 39 | + |
| 40 | +const ( |
| 41 | + KeySelectedOrg ConfigKey = "selected_org" |
| 42 | + KeyOutputFormat ConfigKey = "output_format" |
| 43 | + KeyNoPager ConfigKey = "no_pager" |
| 44 | + KeyQuiet ConfigKey = "quiet" |
| 45 | + KeyNoInput ConfigKey = "no_input" |
| 46 | + KeyPager ConfigKey = "pager" |
| 47 | +) |
| 48 | + |
| 49 | +// AllKeys returns all valid configuration keys |
| 50 | +func AllKeys() []ConfigKey { |
| 51 | + return []ConfigKey{ |
| 52 | + KeySelectedOrg, |
| 53 | + KeyOutputFormat, |
| 54 | + KeyNoPager, |
| 55 | + KeyQuiet, |
| 56 | + KeyNoInput, |
| 57 | + KeyPager, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// ValidateKey checks if a key is valid |
| 62 | +func ValidateKey(key string) (ConfigKey, error) { |
| 63 | + k := ConfigKey(key) |
| 64 | + if slices.Contains(AllKeys(), k) { |
| 65 | + return k, nil |
| 66 | + } |
| 67 | + return "", fmt.Errorf("unknown config key: %s\nvalid keys: %v", key, AllKeys()) |
| 68 | +} |
| 69 | + |
| 70 | +// IsLocalOnly returns true if the key can only be set in user config |
| 71 | +func (k ConfigKey) IsLocalOnly() bool { |
| 72 | + return false |
| 73 | +} |
| 74 | + |
| 75 | +// IsUserOnly returns true if the key can only be set in user config |
| 76 | +func (k ConfigKey) IsUserOnly() bool { |
| 77 | + switch k { |
| 78 | + case KeyNoInput, KeyPager: |
| 79 | + return true |
| 80 | + default: |
| 81 | + return false |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// IsBool returns true if the key is a boolean value |
| 86 | +func (k ConfigKey) IsBool() bool { |
| 87 | + switch k { |
| 88 | + case KeyNoPager, KeyQuiet, KeyNoInput: |
| 89 | + return true |
| 90 | + default: |
| 91 | + return false |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// ValidValues returns valid values for enum keys, or nil if any value is valid |
| 96 | +func (k ConfigKey) ValidValues() []string { |
| 97 | + switch k { |
| 98 | + case KeyOutputFormat: |
| 99 | + return []string{"json", "yaml", "text"} |
| 100 | + case KeyNoPager, KeyQuiet, KeyNoInput: |
| 101 | + return []string{"true", "false"} |
| 102 | + default: |
| 103 | + return nil |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// parseBoolOrDefault parses a boolean string, returning the default for empty strings |
| 108 | +func parseBoolOrDefault(value string, defaultVal bool) (bool, error) { |
| 109 | + if value == "" { |
| 110 | + return defaultVal, nil |
| 111 | + } |
| 112 | + return strconv.ParseBool(value) |
| 113 | +} |
| 114 | + |
| 115 | +func SetConfigValue(conf *config.Config, key ConfigKey, value string, local bool) error { |
| 116 | + switch key { |
| 117 | + case KeySelectedOrg: |
| 118 | + return conf.SelectOrganization(value, local) |
| 119 | + case KeyOutputFormat: |
| 120 | + return conf.SetOutputFormat(value, local) |
| 121 | + case KeyNoPager: |
| 122 | + v, err := parseBoolOrDefault(value, false) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("invalid boolean value %q: %w", value, err) |
| 125 | + } |
| 126 | + return conf.SetNoPager(v, local) |
| 127 | + case KeyQuiet: |
| 128 | + v, err := parseBoolOrDefault(value, false) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("invalid boolean value %q: %w", value, err) |
| 131 | + } |
| 132 | + return conf.SetQuiet(v, local) |
| 133 | + case KeyNoInput: |
| 134 | + v, err := parseBoolOrDefault(value, false) |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("invalid boolean value %q: %w", value, err) |
| 137 | + } |
| 138 | + return conf.SetNoInput(v) |
| 139 | + case KeyPager: |
| 140 | + return conf.SetPager(value) |
| 141 | + } |
| 142 | + |
| 143 | + return nil |
| 144 | +} |
0 commit comments