|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/gnodet/mvx/pkg/config" |
| 9 | + "github.com/gnodet/mvx/pkg/tools" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +// configCmd represents the config command |
| 14 | +var configCmd = &cobra.Command{ |
| 15 | + Use: "config", |
| 16 | + Short: "Manage global mvx configuration", |
| 17 | + Long: `Manage global mvx configuration including URL replacements for enterprise networks. |
| 18 | +
|
| 19 | +The global configuration is stored in ~/.mvx/config.json5 and affects all mvx projects. |
| 20 | +
|
| 21 | +Examples: |
| 22 | + mvx config show # Show current global configuration |
| 23 | + mvx config set-url-replacement github.com nexus.mycompany.net |
| 24 | + mvx config set-url-replacement "regex:^http://(.+)" "https://$1" |
| 25 | + mvx config remove-url-replacement github.com |
| 26 | + mvx config clear-url-replacements |
| 27 | + mvx config edit # Open config file in editor`, |
| 28 | +} |
| 29 | + |
| 30 | +// configShowCmd shows the current global configuration |
| 31 | +var configShowCmd = &cobra.Command{ |
| 32 | + Use: "show", |
| 33 | + Short: "Show current global configuration", |
| 34 | + Run: func(cmd *cobra.Command, args []string) { |
| 35 | + if err := showGlobalConfig(); err != nil { |
| 36 | + printError("Failed to show global configuration: %v", err) |
| 37 | + os.Exit(1) |
| 38 | + } |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +// configSetURLReplacementCmd sets a URL replacement |
| 43 | +var configSetURLReplacementCmd = &cobra.Command{ |
| 44 | + Use: "set-url-replacement <pattern> <replacement>", |
| 45 | + Short: "Set a URL replacement pattern", |
| 46 | + Long: `Set a URL replacement pattern for enterprise networks and mirrors. |
| 47 | +
|
| 48 | +Examples: |
| 49 | + # Simple hostname replacement |
| 50 | + mvx config set-url-replacement github.com nexus.mycompany.net |
| 51 | + |
| 52 | + # Regex replacement (upgrade HTTP to HTTPS) |
| 53 | + mvx config set-url-replacement "regex:^http://(.+)" "https://$1" |
| 54 | + |
| 55 | + # GitHub releases mirror |
| 56 | + mvx config set-url-replacement "regex:https://github\\.com/([^/]+)/([^/]+)/releases/download/(.+)" "https://hub.corp.com/artifactory/github/$1/$2/$3"`, |
| 57 | + Args: cobra.ExactArgs(2), |
| 58 | + Run: func(cmd *cobra.Command, args []string) { |
| 59 | + pattern := args[0] |
| 60 | + replacement := args[1] |
| 61 | + if err := setURLReplacement(pattern, replacement); err != nil { |
| 62 | + printError("Failed to set URL replacement: %v", err) |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +// configRemoveURLReplacementCmd removes a URL replacement |
| 69 | +var configRemoveURLReplacementCmd = &cobra.Command{ |
| 70 | + Use: "remove-url-replacement <pattern>", |
| 71 | + Short: "Remove a URL replacement pattern", |
| 72 | + Args: cobra.ExactArgs(1), |
| 73 | + Run: func(cmd *cobra.Command, args []string) { |
| 74 | + pattern := args[0] |
| 75 | + if err := removeURLReplacement(pattern); err != nil { |
| 76 | + printError("Failed to remove URL replacement: %v", err) |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + }, |
| 80 | +} |
| 81 | + |
| 82 | +// configClearURLReplacementsCmd clears all URL replacements |
| 83 | +var configClearURLReplacementsCmd = &cobra.Command{ |
| 84 | + Use: "clear-url-replacements", |
| 85 | + Short: "Clear all URL replacement patterns", |
| 86 | + Run: func(cmd *cobra.Command, args []string) { |
| 87 | + if err := clearURLReplacements(); err != nil { |
| 88 | + printError("Failed to clear URL replacements: %v", err) |
| 89 | + os.Exit(1) |
| 90 | + } |
| 91 | + }, |
| 92 | +} |
| 93 | + |
| 94 | +// configEditCmd opens the global config file in an editor |
| 95 | +var configEditCmd = &cobra.Command{ |
| 96 | + Use: "edit", |
| 97 | + Short: "Open global configuration file in editor", |
| 98 | + Run: func(cmd *cobra.Command, args []string) { |
| 99 | + if err := editGlobalConfig(); err != nil { |
| 100 | + printError("Failed to edit global configuration: %v", err) |
| 101 | + os.Exit(1) |
| 102 | + } |
| 103 | + }, |
| 104 | +} |
| 105 | + |
| 106 | +func init() { |
| 107 | + // Add subcommands |
| 108 | + configCmd.AddCommand(configShowCmd) |
| 109 | + configCmd.AddCommand(configSetURLReplacementCmd) |
| 110 | + configCmd.AddCommand(configRemoveURLReplacementCmd) |
| 111 | + configCmd.AddCommand(configClearURLReplacementsCmd) |
| 112 | + configCmd.AddCommand(configEditCmd) |
| 113 | +} |
| 114 | + |
| 115 | +func showGlobalConfig() error { |
| 116 | + cfg, err := config.LoadGlobalConfig() |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + configPath, err := config.GetGlobalConfigPath() |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + printInfo("Global mvx configuration (%s):", configPath) |
| 127 | + printInfo("") |
| 128 | + |
| 129 | + if len(cfg.URLReplacements) == 0 { |
| 130 | + printInfo("No URL replacements configured.") |
| 131 | + printInfo("") |
| 132 | + printInfo("To add URL replacements:") |
| 133 | + printInfo(" mvx config set-url-replacement github.com nexus.mycompany.net") |
| 134 | + printInfo(" mvx config set-url-replacement \"regex:^http://(.+)\" \"https://$1\"") |
| 135 | + } else { |
| 136 | + printInfo("URL Replacements:") |
| 137 | + for pattern, replacement := range cfg.URLReplacements { |
| 138 | + if strings.HasPrefix(pattern, "regex:") { |
| 139 | + printInfo(" %s -> %s (regex)", pattern, replacement) |
| 140 | + } else { |
| 141 | + printInfo(" %s -> %s", pattern, replacement) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + printInfo("") |
| 147 | + printInfo("Examples:") |
| 148 | + printInfo(" mvx config set-url-replacement github.com nexus.mycompany.net") |
| 149 | + printInfo(" mvx config remove-url-replacement github.com") |
| 150 | + printInfo(" mvx config clear-url-replacements") |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func setURLReplacement(pattern, replacement string) error { |
| 156 | + cfg, err := config.LoadGlobalConfig() |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + // Initialize map if nil |
| 162 | + if cfg.URLReplacements == nil { |
| 163 | + cfg.URLReplacements = make(map[string]string) |
| 164 | + } |
| 165 | + |
| 166 | + // Validate regex patterns |
| 167 | + if strings.HasPrefix(pattern, "regex:") { |
| 168 | + replacer := tools.NewURLReplacer(map[string]string{pattern: replacement}) |
| 169 | + if errors := replacer.ValidateReplacements(); len(errors) > 0 { |
| 170 | + return fmt.Errorf("invalid regex pattern: %v", errors[0]) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + cfg.URLReplacements[pattern] = replacement |
| 175 | + |
| 176 | + if err := config.SaveGlobalConfig(cfg); err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + |
| 180 | + printSuccess("✅ URL replacement added: %s -> %s", pattern, replacement) |
| 181 | + if strings.HasPrefix(pattern, "regex:") { |
| 182 | + printInfo(" Pattern type: regex") |
| 183 | + } else { |
| 184 | + printInfo(" Pattern type: simple string replacement") |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
| 189 | + |
| 190 | +func removeURLReplacement(pattern string) error { |
| 191 | + cfg, err := config.LoadGlobalConfig() |
| 192 | + if err != nil { |
| 193 | + return err |
| 194 | + } |
| 195 | + |
| 196 | + if cfg.URLReplacements == nil || len(cfg.URLReplacements) == 0 { |
| 197 | + printInfo("No URL replacements configured.") |
| 198 | + return nil |
| 199 | + } |
| 200 | + |
| 201 | + if _, exists := cfg.URLReplacements[pattern]; !exists { |
| 202 | + printInfo("URL replacement pattern '%s' not found.", pattern) |
| 203 | + return nil |
| 204 | + } |
| 205 | + |
| 206 | + delete(cfg.URLReplacements, pattern) |
| 207 | + |
| 208 | + if err := config.SaveGlobalConfig(cfg); err != nil { |
| 209 | + return err |
| 210 | + } |
| 211 | + |
| 212 | + printSuccess("✅ URL replacement removed: %s", pattern) |
| 213 | + return nil |
| 214 | +} |
| 215 | + |
| 216 | +func clearURLReplacements() error { |
| 217 | + cfg, err := config.LoadGlobalConfig() |
| 218 | + if err != nil { |
| 219 | + return err |
| 220 | + } |
| 221 | + |
| 222 | + if cfg.URLReplacements == nil || len(cfg.URLReplacements) == 0 { |
| 223 | + printInfo("No URL replacements configured.") |
| 224 | + return nil |
| 225 | + } |
| 226 | + |
| 227 | + count := len(cfg.URLReplacements) |
| 228 | + cfg.URLReplacements = make(map[string]string) |
| 229 | + |
| 230 | + if err := config.SaveGlobalConfig(cfg); err != nil { |
| 231 | + return err |
| 232 | + } |
| 233 | + |
| 234 | + printSuccess("✅ Cleared %d URL replacement(s)", count) |
| 235 | + return nil |
| 236 | +} |
| 237 | + |
| 238 | +func editGlobalConfig() error { |
| 239 | + configPath, err := config.GetGlobalConfigPath() |
| 240 | + if err != nil { |
| 241 | + return err |
| 242 | + } |
| 243 | + |
| 244 | + // Ensure config file exists |
| 245 | + cfg, err := config.LoadGlobalConfig() |
| 246 | + if err != nil { |
| 247 | + return err |
| 248 | + } |
| 249 | + |
| 250 | + if err := config.SaveGlobalConfig(cfg); err != nil { |
| 251 | + return err |
| 252 | + } |
| 253 | + |
| 254 | + // Try to open with common editors |
| 255 | + editors := []string{ |
| 256 | + os.Getenv("EDITOR"), |
| 257 | + "code", // VS Code |
| 258 | + "vim", |
| 259 | + "nano", |
| 260 | + "notepad", // Windows |
| 261 | + } |
| 262 | + |
| 263 | + for _, editor := range editors { |
| 264 | + if editor == "" { |
| 265 | + continue |
| 266 | + } |
| 267 | + |
| 268 | + printInfo("Opening %s with %s...", configPath, editor) |
| 269 | + // Note: In a real implementation, you'd use exec.Command here |
| 270 | + // For now, just show the path |
| 271 | + printInfo("Please edit: %s", configPath) |
| 272 | + return nil |
| 273 | + } |
| 274 | + |
| 275 | + printInfo("Please edit the configuration file manually: %s", configPath) |
| 276 | + return nil |
| 277 | +} |
0 commit comments