|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/spf13/viper" |
| 12 | +) |
| 13 | + |
| 14 | +var cfgFile string |
| 15 | + |
| 16 | +// configCmd is the parent command for config management |
| 17 | +var configCmd = &cobra.Command{ |
| 18 | + Use: "config", |
| 19 | + Short: "Manage configuration settings", |
| 20 | + Long: `View and modify MAP configuration settings stored in ~/.mapd/config.yaml.`, |
| 21 | +} |
| 22 | + |
| 23 | +var configListCmd = &cobra.Command{ |
| 24 | + Use: "list", |
| 25 | + Aliases: []string{"ls"}, |
| 26 | + Short: "List all configuration values", |
| 27 | + Long: `Display all configuration values including defaults and overrides.`, |
| 28 | + RunE: runConfigList, |
| 29 | +} |
| 30 | + |
| 31 | +var configGetCmd = &cobra.Command{ |
| 32 | + Use: "get <key>", |
| 33 | + Short: "Get a configuration value", |
| 34 | + Long: `Get a specific configuration value by key. |
| 35 | +
|
| 36 | +Examples: |
| 37 | + map config get socket |
| 38 | + map config get agent.default-type |
| 39 | + map config get agent.default-count`, |
| 40 | + Args: cobra.ExactArgs(1), |
| 41 | + RunE: runConfigGet, |
| 42 | +} |
| 43 | + |
| 44 | +var configSetCmd = &cobra.Command{ |
| 45 | + Use: "set <key> <value>", |
| 46 | + Short: "Set a configuration value", |
| 47 | + Long: `Set a configuration value and persist it to ~/.mapd/config.yaml. |
| 48 | +
|
| 49 | +Examples: |
| 50 | + map config set socket /custom/path.sock |
| 51 | + map config set agent.default-type codex |
| 52 | + map config set agent.default-count 3 |
| 53 | + map config set agent.use-worktree false`, |
| 54 | + Args: cobra.ExactArgs(2), |
| 55 | + RunE: runConfigSet, |
| 56 | +} |
| 57 | + |
| 58 | +func init() { |
| 59 | + rootCmd.AddCommand(configCmd) |
| 60 | + configCmd.AddCommand(configListCmd) |
| 61 | + configCmd.AddCommand(configGetCmd) |
| 62 | + configCmd.AddCommand(configSetCmd) |
| 63 | +} |
| 64 | + |
| 65 | +// initConfig reads in config file and ENV variables if set |
| 66 | +func initConfig() error { |
| 67 | + // Set defaults |
| 68 | + viper.SetDefault("socket", "/tmp/mapd.sock") |
| 69 | + viper.SetDefault("data-dir", filepath.Join(os.Getenv("HOME"), ".mapd")) |
| 70 | + viper.SetDefault("agent.default-type", "claude") |
| 71 | + viper.SetDefault("agent.default-count", 1) |
| 72 | + viper.SetDefault("agent.default-branch", "") |
| 73 | + viper.SetDefault("agent.use-worktree", true) |
| 74 | + viper.SetDefault("agent.skip-permissions", true) |
| 75 | + |
| 76 | + if cfgFile != "" { |
| 77 | + // Use config file from the flag |
| 78 | + viper.SetConfigFile(cfgFile) |
| 79 | + } else { |
| 80 | + // Search for config in ~/.mapd directory |
| 81 | + home, err := os.UserHomeDir() |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("get home directory: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + configDir := filepath.Join(home, ".mapd") |
| 87 | + viper.AddConfigPath(configDir) |
| 88 | + viper.SetConfigType("yaml") |
| 89 | + viper.SetConfigName("config") |
| 90 | + } |
| 91 | + |
| 92 | + // Environment variables with MAP_ prefix |
| 93 | + viper.SetEnvPrefix("MAP") |
| 94 | + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) |
| 95 | + viper.AutomaticEnv() |
| 96 | + |
| 97 | + // Read config file (ignore if not found) |
| 98 | + if err := viper.ReadInConfig(); err != nil { |
| 99 | + if _, ok := err.(viper.ConfigFileNotFoundError); !ok { |
| 100 | + return fmt.Errorf("read config: %w", err) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Bind the socket flag to viper |
| 105 | + if err := viper.BindPFlag("socket", rootCmd.PersistentFlags().Lookup("socket")); err != nil { |
| 106 | + return fmt.Errorf("bind socket flag: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +// writeConfig writes the current configuration to the config file |
| 113 | +func writeConfig() error { |
| 114 | + home, err := os.UserHomeDir() |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("get home directory: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + configDir := filepath.Join(home, ".mapd") |
| 120 | + if err := os.MkdirAll(configDir, 0755); err != nil { |
| 121 | + return fmt.Errorf("create config directory: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + configPath := filepath.Join(configDir, "config.yaml") |
| 125 | + if err := viper.WriteConfigAs(configPath); err != nil { |
| 126 | + return fmt.Errorf("write config: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func runConfigList(cmd *cobra.Command, args []string) error { |
| 133 | + keys := viper.AllKeys() |
| 134 | + sort.Strings(keys) |
| 135 | + |
| 136 | + fmt.Printf("%-25s %s\n", "KEY", "VALUE") |
| 137 | + fmt.Println(strings.Repeat("-", 50)) |
| 138 | + |
| 139 | + for _, key := range keys { |
| 140 | + value := viper.Get(key) |
| 141 | + fmt.Printf("%-25s %v\n", key, value) |
| 142 | + } |
| 143 | + |
| 144 | + // Show config file location if it exists |
| 145 | + if viper.ConfigFileUsed() != "" { |
| 146 | + fmt.Printf("\nConfig file: %s\n", viper.ConfigFileUsed()) |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func runConfigGet(cmd *cobra.Command, args []string) error { |
| 153 | + key := args[0] |
| 154 | + |
| 155 | + if !viper.IsSet(key) { |
| 156 | + return fmt.Errorf("key %q not found", key) |
| 157 | + } |
| 158 | + |
| 159 | + fmt.Println(viper.Get(key)) |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func runConfigSet(cmd *cobra.Command, args []string) error { |
| 164 | + key := args[0] |
| 165 | + value := args[1] |
| 166 | + |
| 167 | + // Handle boolean values |
| 168 | + switch strings.ToLower(value) { |
| 169 | + case "true": |
| 170 | + viper.Set(key, true) |
| 171 | + case "false": |
| 172 | + viper.Set(key, false) |
| 173 | + default: |
| 174 | + // Try to parse as integer |
| 175 | + var intVal int |
| 176 | + if _, err := fmt.Sscanf(value, "%d", &intVal); err == nil { |
| 177 | + viper.Set(key, intVal) |
| 178 | + } else { |
| 179 | + viper.Set(key, value) |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if err := writeConfig(); err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + |
| 187 | + fmt.Printf("set %s = %v\n", key, viper.Get(key)) |
| 188 | + return nil |
| 189 | +} |
0 commit comments