|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/qoliber/magebox/internal/cli" |
| 10 | + "github.com/qoliber/magebox/internal/config" |
| 11 | +) |
| 12 | + |
| 13 | +var configCmd = &cobra.Command{ |
| 14 | + Use: "config", |
| 15 | + Short: "MageBox configuration", |
| 16 | + Long: "View and modify MageBox global configuration", |
| 17 | +} |
| 18 | + |
| 19 | +var configShowCmd = &cobra.Command{ |
| 20 | + Use: "show", |
| 21 | + Short: "Show current configuration", |
| 22 | + Long: "Displays the current MageBox global configuration", |
| 23 | + RunE: runConfigShow, |
| 24 | +} |
| 25 | + |
| 26 | +var configSetCmd = &cobra.Command{ |
| 27 | + Use: "set <key> <value>", |
| 28 | + Short: "Set a configuration value", |
| 29 | + Long: `Sets a global configuration value. |
| 30 | +
|
| 31 | +Available keys: |
| 32 | + dns_mode - DNS resolution mode: "hosts" or "dnsmasq" |
| 33 | + default_php - Default PHP version for new projects (e.g., "8.2") |
| 34 | + tld - Top-level domain for local dev (default: "test") |
| 35 | + portainer - Enable Portainer Docker UI: "true" or "false"`, |
| 36 | + Args: cobra.ExactArgs(2), |
| 37 | + RunE: runConfigSet, |
| 38 | +} |
| 39 | + |
| 40 | +var configInitCmd = &cobra.Command{ |
| 41 | + Use: "init", |
| 42 | + Short: "Initialize global configuration", |
| 43 | + Long: "Creates the global configuration file with defaults", |
| 44 | + RunE: runConfigInit, |
| 45 | +} |
| 46 | + |
| 47 | +func init() { |
| 48 | + configCmd.AddCommand(configShowCmd) |
| 49 | + configCmd.AddCommand(configSetCmd) |
| 50 | + configCmd.AddCommand(configInitCmd) |
| 51 | + rootCmd.AddCommand(configCmd) |
| 52 | +} |
| 53 | + |
| 54 | +func runConfigShow(cmd *cobra.Command, args []string) error { |
| 55 | + homeDir, err := os.UserHomeDir() |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + cfg, err := config.LoadGlobalConfig(homeDir) |
| 61 | + if err != nil { |
| 62 | + cli.PrintError("Failed to load config: %v", err) |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + cli.PrintTitle("MageBox Global Configuration") |
| 67 | + fmt.Println() |
| 68 | + |
| 69 | + configPath := config.GlobalConfigPath(homeDir) |
| 70 | + if config.GlobalConfigExists(homeDir) { |
| 71 | + fmt.Printf("Config file: %s\n", cli.Path(configPath)) |
| 72 | + } else { |
| 73 | + fmt.Printf("Config file: %s (using defaults)\n", cli.Subtitle("not created")) |
| 74 | + } |
| 75 | + fmt.Println() |
| 76 | + |
| 77 | + fmt.Printf(" %-14s %s\n", "dns_mode:", cli.Highlight(cfg.DNSMode)) |
| 78 | + fmt.Printf(" %-14s %s\n", "default_php:", cli.Highlight(cfg.DefaultPHP)) |
| 79 | + fmt.Printf(" %-14s %s\n", "tld:", cli.Highlight(cfg.TLD)) |
| 80 | + fmt.Printf(" %-14s %s\n", "portainer:", cli.Highlight(fmt.Sprintf("%v", cfg.Portainer))) |
| 81 | + fmt.Printf(" %-14s %s\n", "auto_start:", cli.Highlight(fmt.Sprintf("%v", cfg.AutoStart))) |
| 82 | + |
| 83 | + fmt.Println(cli.Header("Default Services")) |
| 84 | + if cfg.DefaultServices.MySQL != "" { |
| 85 | + fmt.Printf(" %-14s %s\n", "mysql:", cli.Highlight(cfg.DefaultServices.MySQL)) |
| 86 | + } |
| 87 | + if cfg.DefaultServices.MariaDB != "" { |
| 88 | + fmt.Printf(" %-14s %s\n", "mariadb:", cli.Highlight(cfg.DefaultServices.MariaDB)) |
| 89 | + } |
| 90 | + fmt.Printf(" %-14s %s\n", "redis:", cli.Highlight(fmt.Sprintf("%v", cfg.DefaultServices.Redis))) |
| 91 | + if cfg.DefaultServices.OpenSearch != "" { |
| 92 | + fmt.Printf(" %-14s %s\n", "opensearch:", cli.Highlight(cfg.DefaultServices.OpenSearch)) |
| 93 | + } |
| 94 | + fmt.Printf(" %-14s %s\n", "rabbitmq:", cli.Highlight(fmt.Sprintf("%v", cfg.DefaultServices.RabbitMQ))) |
| 95 | + fmt.Printf(" %-14s %s\n", "mailpit:", cli.Highlight(fmt.Sprintf("%v", cfg.DefaultServices.Mailpit))) |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func runConfigSet(cmd *cobra.Command, args []string) error { |
| 101 | + key := args[0] |
| 102 | + value := args[1] |
| 103 | + |
| 104 | + homeDir, err := os.UserHomeDir() |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + cfg, err := config.LoadGlobalConfig(homeDir) |
| 110 | + if err != nil { |
| 111 | + cli.PrintError("Failed to load config: %v", err) |
| 112 | + return nil |
| 113 | + } |
| 114 | + |
| 115 | + switch key { |
| 116 | + case "dns_mode": |
| 117 | + if value != "hosts" && value != "dnsmasq" { |
| 118 | + cli.PrintError("Invalid value for dns_mode. Use 'hosts' or 'dnsmasq'") |
| 119 | + return nil |
| 120 | + } |
| 121 | + cfg.DNSMode = value |
| 122 | + case "default_php": |
| 123 | + cfg.DefaultPHP = value |
| 124 | + case "tld": |
| 125 | + cfg.TLD = value |
| 126 | + case "portainer": |
| 127 | + cfg.Portainer = (value == "true" || value == "1" || value == "yes") |
| 128 | + case "auto_start": |
| 129 | + cfg.AutoStart = (value == "true" || value == "1" || value == "yes") |
| 130 | + default: |
| 131 | + cli.PrintError("Unknown configuration key: %s", key) |
| 132 | + fmt.Println() |
| 133 | + cli.PrintInfo("Available keys: dns_mode, default_php, tld, portainer, auto_start") |
| 134 | + return nil |
| 135 | + } |
| 136 | + |
| 137 | + if err := config.SaveGlobalConfig(homeDir, cfg); err != nil { |
| 138 | + cli.PrintError("Failed to save config: %v", err) |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + cli.PrintSuccess("Configuration updated: %s = %s", key, value) |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func runConfigInit(cmd *cobra.Command, args []string) error { |
| 147 | + homeDir, err := os.UserHomeDir() |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + |
| 152 | + configPath := config.GlobalConfigPath(homeDir) |
| 153 | + |
| 154 | + if config.GlobalConfigExists(homeDir) { |
| 155 | + cli.PrintWarning("Configuration already exists at %s", configPath) |
| 156 | + return nil |
| 157 | + } |
| 158 | + |
| 159 | + if err := config.InitGlobalConfig(homeDir); err != nil { |
| 160 | + cli.PrintError("Failed to initialize config: %v", err) |
| 161 | + return nil |
| 162 | + } |
| 163 | + |
| 164 | + cli.PrintSuccess("Created global configuration at %s", configPath) |
| 165 | + fmt.Println() |
| 166 | + cli.PrintInfo("Edit the file or use " + cli.Command("magebox config set <key> <value>")) |
| 167 | + return nil |
| 168 | +} |
0 commit comments