|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "gettrackers/internal/config" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var configCmd = &cobra.Command{ |
| 14 | + Use: "config", |
| 15 | + Short: "Manage configuration", |
| 16 | + Long: `Get or set configuration values.`, |
| 17 | +} |
| 18 | + |
| 19 | +var configGetCmd = &cobra.Command{ |
| 20 | + Use: "get [key]", |
| 21 | + Short: "Show config values", |
| 22 | + Long: `Show all config values if no key is specified, or a specific value if key is given.`, |
| 23 | + Args: cobra.MaximumNArgs(1), |
| 24 | + RunE: runConfigGet, |
| 25 | +} |
| 26 | + |
| 27 | +var configSetCmd = &cobra.Command{ |
| 28 | + Use: "set <key> <value>", |
| 29 | + Short: "Set a config value", |
| 30 | + Long: `Set a configuration value. For source_urls, provide comma-separated URLs.`, |
| 31 | + Args: cobra.ExactArgs(2), |
| 32 | + RunE: runConfigSet, |
| 33 | +} |
| 34 | + |
| 35 | +func init() { |
| 36 | + rootCmd.AddCommand(configCmd) |
| 37 | + configCmd.AddCommand(configGetCmd) |
| 38 | + configCmd.AddCommand(configSetCmd) |
| 39 | +} |
| 40 | + |
| 41 | +func runConfigGet(cmd *cobra.Command, args []string) error { |
| 42 | + cfg, err := config.Load() |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("failed to load config: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + if len(args) == 0 { |
| 48 | + // Show all config |
| 49 | + fmt.Printf("source_urls:\n") |
| 50 | + for _, url := range cfg.SourceURLs { |
| 51 | + fmt.Printf(" - %s\n", url) |
| 52 | + } |
| 53 | + return nil |
| 54 | + } |
| 55 | + |
| 56 | + // Show specific key |
| 57 | + key := args[0] |
| 58 | + value, err := cfg.Get(key) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + switch v := value.(type) { |
| 64 | + case []string: |
| 65 | + for _, item := range v { |
| 66 | + fmt.Println(item) |
| 67 | + } |
| 68 | + default: |
| 69 | + fmt.Println(value) |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func runConfigSet(cmd *cobra.Command, args []string) error { |
| 76 | + cfg, err := config.Load() |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("failed to load config: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + key := args[0] |
| 82 | + valueStr := args[1] |
| 83 | + |
| 84 | + var value interface{} |
| 85 | + switch key { |
| 86 | + case "source_urls": |
| 87 | + // Split by comma and trim spaces |
| 88 | + urls := strings.Split(valueStr, ",") |
| 89 | + trimmed := make([]string, 0, len(urls)) |
| 90 | + for _, url := range urls { |
| 91 | + trimmed = append(trimmed, strings.TrimSpace(url)) |
| 92 | + } |
| 93 | + value = trimmed |
| 94 | + default: |
| 95 | + value = valueStr |
| 96 | + } |
| 97 | + |
| 98 | + if err := cfg.Set(key, value); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + if err := cfg.Save(); err != nil { |
| 103 | + return fmt.Errorf("failed to save config: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + fmt.Fprintf(os.Stderr, "Set %s = %v\n", key, value) |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
0 commit comments