|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/DeusData/codebase-memory-mcp/internal/store" |
| 10 | +) |
| 11 | + |
| 12 | +// configDefaults documents all known config keys, their defaults, and descriptions. |
| 13 | +var configDefaults = []struct { |
| 14 | + Key string |
| 15 | + Default string |
| 16 | + Description string |
| 17 | +}{ |
| 18 | + {store.ConfigAutoIndex, "false", "Enable background auto-indexing on MCP session start"}, |
| 19 | + {store.ConfigAutoIndexLimit, "50000", "Max files for auto-indexing new (never-indexed) projects"}, |
| 20 | + {store.ConfigMemLimit, "", "GOMEMLIMIT for the server process (e.g. 2G, 512M). Empty = no limit"}, |
| 21 | +} |
| 22 | + |
| 23 | +func runConfig(args []string) int { |
| 24 | + if len(args) == 0 { |
| 25 | + printConfigHelp() |
| 26 | + return 0 |
| 27 | + } |
| 28 | + |
| 29 | + switch args[0] { |
| 30 | + case "list", "ls": |
| 31 | + return configList() |
| 32 | + case "get": |
| 33 | + if len(args) < 2 { |
| 34 | + fmt.Fprintln(os.Stderr, "Usage: codebase-memory-mcp config get <key>") |
| 35 | + return 1 |
| 36 | + } |
| 37 | + return configGet(args[1]) |
| 38 | + case "set": |
| 39 | + if len(args) < 3 { |
| 40 | + fmt.Fprintln(os.Stderr, "Usage: codebase-memory-mcp config set <key> <value>") |
| 41 | + return 1 |
| 42 | + } |
| 43 | + return configSet(args[1], args[2]) |
| 44 | + case "reset": |
| 45 | + if len(args) < 2 { |
| 46 | + fmt.Fprintln(os.Stderr, "Usage: codebase-memory-mcp config reset <key>") |
| 47 | + return 1 |
| 48 | + } |
| 49 | + return configReset(args[1]) |
| 50 | + case "--help", "-h", "help": |
| 51 | + printConfigHelp() |
| 52 | + return 0 |
| 53 | + default: |
| 54 | + fmt.Fprintf(os.Stderr, "Unknown config command: %s\n", args[0]) |
| 55 | + printConfigHelp() |
| 56 | + return 1 |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func configList() int { |
| 61 | + cfg, err := store.OpenConfig() |
| 62 | + if err != nil { |
| 63 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 64 | + return 1 |
| 65 | + } |
| 66 | + defer cfg.Close() |
| 67 | + |
| 68 | + all, err := cfg.All() |
| 69 | + if err != nil { |
| 70 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 71 | + return 1 |
| 72 | + } |
| 73 | + |
| 74 | + // Merge with defaults to show all known keys |
| 75 | + merged := make(map[string]string) |
| 76 | + for _, d := range configDefaults { |
| 77 | + merged[d.Key] = d.Default |
| 78 | + } |
| 79 | + for k, v := range all { |
| 80 | + merged[k] = v |
| 81 | + } |
| 82 | + |
| 83 | + keys := make([]string, 0, len(merged)) |
| 84 | + for k := range merged { |
| 85 | + keys = append(keys, k) |
| 86 | + } |
| 87 | + sort.Strings(keys) |
| 88 | + |
| 89 | + fmt.Println("Configuration:") |
| 90 | + for _, k := range keys { |
| 91 | + v := merged[k] |
| 92 | + source := "default" |
| 93 | + if _, ok := all[k]; ok { |
| 94 | + source = "set" |
| 95 | + } |
| 96 | + desc := configDescription(k) |
| 97 | + fmt.Printf(" %-25s = %-10s (%s) %s\n", k, v, source, desc) |
| 98 | + } |
| 99 | + return 0 |
| 100 | +} |
| 101 | + |
| 102 | +func configGet(key string) int { |
| 103 | + cfg, err := store.OpenConfig() |
| 104 | + if err != nil { |
| 105 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 106 | + return 1 |
| 107 | + } |
| 108 | + defer cfg.Close() |
| 109 | + |
| 110 | + defVal := configDefaultValue(key) |
| 111 | + val := cfg.Get(key, defVal) |
| 112 | + fmt.Println(val) |
| 113 | + return 0 |
| 114 | +} |
| 115 | + |
| 116 | +func configSet(key, value string) int { |
| 117 | + // Validate known keys |
| 118 | + if !isKnownConfigKey(key) { |
| 119 | + fmt.Fprintf(os.Stderr, "Unknown config key: %s\n", key) |
| 120 | + fmt.Fprintf(os.Stderr, "Known keys: %s\n", knownConfigKeys()) |
| 121 | + return 1 |
| 122 | + } |
| 123 | + |
| 124 | + // Validate mem_limit |
| 125 | + if key == store.ConfigMemLimit && value != "" { |
| 126 | + if _, err := parseByteSize(value); err != nil { |
| 127 | + fmt.Fprintf(os.Stderr, "Invalid value for %s: %q (expected size like 2G, 512M, 4096M)\n", key, value) |
| 128 | + return 1 |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Validate bool keys |
| 133 | + if key == store.ConfigAutoIndex { |
| 134 | + v := strings.ToLower(value) |
| 135 | + if v != "true" && v != "false" && v != "on" && v != "off" && v != "1" && v != "0" { |
| 136 | + fmt.Fprintf(os.Stderr, "Invalid value for %s: %q (expected true/false)\n", key, value) |
| 137 | + return 1 |
| 138 | + } |
| 139 | + // Normalize |
| 140 | + switch v { |
| 141 | + case "on", "1": |
| 142 | + value = "true" |
| 143 | + case "off", "0": |
| 144 | + value = "false" |
| 145 | + default: |
| 146 | + value = v |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + cfg, err := store.OpenConfig() |
| 151 | + if err != nil { |
| 152 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 153 | + return 1 |
| 154 | + } |
| 155 | + defer cfg.Close() |
| 156 | + |
| 157 | + if err := cfg.Set(key, value); err != nil { |
| 158 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 159 | + return 1 |
| 160 | + } |
| 161 | + fmt.Printf("%s = %s\n", key, value) |
| 162 | + return 0 |
| 163 | +} |
| 164 | + |
| 165 | +func configReset(key string) int { |
| 166 | + cfg, err := store.OpenConfig() |
| 167 | + if err != nil { |
| 168 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 169 | + return 1 |
| 170 | + } |
| 171 | + defer cfg.Close() |
| 172 | + |
| 173 | + if err := cfg.Delete(key); err != nil { |
| 174 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 175 | + return 1 |
| 176 | + } |
| 177 | + fmt.Printf("%s reset to default (%s)\n", key, configDefaultValue(key)) |
| 178 | + return 0 |
| 179 | +} |
| 180 | + |
| 181 | +func printConfigHelp() { |
| 182 | + fmt.Fprintf(os.Stderr, `Usage: codebase-memory-mcp config <command> [args] |
| 183 | +
|
| 184 | +Commands: |
| 185 | + list Show all config values (with defaults) |
| 186 | + get <key> Get a config value |
| 187 | + set <key> <val> Set a config value |
| 188 | + reset <key> Reset a key to its default |
| 189 | +
|
| 190 | +Config keys: |
| 191 | +`) |
| 192 | + for _, d := range configDefaults { |
| 193 | + fmt.Fprintf(os.Stderr, " %-25s default=%-10s %s\n", d.Key, d.Default, d.Description) |
| 194 | + } |
| 195 | + fmt.Fprintf(os.Stderr, ` |
| 196 | +Examples: |
| 197 | + codebase-memory-mcp config set auto_index true Enable auto-indexing on session start |
| 198 | + codebase-memory-mcp config set auto_index false Disable auto-indexing (default) |
| 199 | + codebase-memory-mcp config set auto_index_limit 20000 |
| 200 | + codebase-memory-mcp config list Show all settings |
| 201 | +`) |
| 202 | +} |
| 203 | + |
| 204 | +func configDefaultValue(key string) string { |
| 205 | + for _, d := range configDefaults { |
| 206 | + if d.Key == key { |
| 207 | + return d.Default |
| 208 | + } |
| 209 | + } |
| 210 | + return "" |
| 211 | +} |
| 212 | + |
| 213 | +func configDescription(key string) string { |
| 214 | + for _, d := range configDefaults { |
| 215 | + if d.Key == key { |
| 216 | + return d.Description |
| 217 | + } |
| 218 | + } |
| 219 | + return "" |
| 220 | +} |
| 221 | + |
| 222 | +func isKnownConfigKey(key string) bool { |
| 223 | + for _, d := range configDefaults { |
| 224 | + if d.Key == key { |
| 225 | + return true |
| 226 | + } |
| 227 | + } |
| 228 | + return false |
| 229 | +} |
| 230 | + |
| 231 | +func knownConfigKeys() string { |
| 232 | + keys := make([]string, len(configDefaults)) |
| 233 | + for i, d := range configDefaults { |
| 234 | + keys[i] = d.Key |
| 235 | + } |
| 236 | + return strings.Join(keys, ", ") |
| 237 | +} |
0 commit comments