|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "sort" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/bmf-san/ggc/config" |
| 12 | +) |
| 13 | + |
| 14 | +// Configureer handles config operations. |
| 15 | +type Configureer struct { |
| 16 | + outputWriter io.Writer |
| 17 | + helper *Helper |
| 18 | + execCommand func(string, ...string) *exec.Cmd |
| 19 | +} |
| 20 | + |
| 21 | +// NewConfigureer creates a new Configureer instance. |
| 22 | +func NewConfigureer() *Configureer { |
| 23 | + return &Configureer{ |
| 24 | + outputWriter: os.Stdout, |
| 25 | + helper: NewHelper(), |
| 26 | + execCommand: exec.Command, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// LoadConfig executes loads the configuration. |
| 31 | +func (c *Configureer) LoadConfig() *config.Manager { |
| 32 | + cm := config.NewConfigManager() |
| 33 | + if err := cm.Load(); err != nil { |
| 34 | + _, _ = fmt.Fprintf(c.outputWriter, "failed to load config: %s", err) |
| 35 | + return nil |
| 36 | + } |
| 37 | + return cm |
| 38 | +} |
| 39 | + |
| 40 | +// Config executes config command operations with the given arguments. |
| 41 | +func (c *Configureer) Config(args []string) { |
| 42 | + if len(args) == 0 { |
| 43 | + c.helper.ShowConfigHelp() |
| 44 | + return |
| 45 | + } |
| 46 | + switch args[0] { |
| 47 | + case "list": |
| 48 | + cm := c.LoadConfig() |
| 49 | + configs := cm.List() |
| 50 | + |
| 51 | + keys := make([]string, 0, len(configs)) |
| 52 | + for key := range configs { |
| 53 | + keys = append(keys, key) |
| 54 | + } |
| 55 | + sort.Strings(keys) |
| 56 | + |
| 57 | + for _, key := range keys { |
| 58 | + _, _ = fmt.Fprintf(c.outputWriter, "%-30s = %s\n", key, formatValue(configs[key])) |
| 59 | + } |
| 60 | + return |
| 61 | + case "get": |
| 62 | + if len(args) < 2 { |
| 63 | + _, _ = fmt.Fprintf(c.outputWriter, "must provide key to get (arg missing)\n") |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + cm := c.LoadConfig() |
| 68 | + |
| 69 | + value, err := cm.Get(args[1]) |
| 70 | + if err != nil { |
| 71 | + _, _ = fmt.Fprintf(c.outputWriter, "failed to get config value: %s", err) |
| 72 | + } |
| 73 | + |
| 74 | + _, _ = fmt.Fprintf(c.outputWriter, "%s\n", formatValue(value)) |
| 75 | + return |
| 76 | + case "set": |
| 77 | + if len(args) < 3 { |
| 78 | + _, _ = fmt.Fprintf(c.outputWriter, "must provide key && value to set (arg(s) missing)\n") |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + cm := c.LoadConfig() |
| 83 | + |
| 84 | + value := parseValue(args[2]) |
| 85 | + if err := cm.Set(args[1], value); err != nil { |
| 86 | + _, _ = fmt.Fprintf(c.outputWriter, "failed to set config value: %s", err) |
| 87 | + } |
| 88 | + |
| 89 | + _, _ = fmt.Fprintf(c.outputWriter, "Set %s = %s\n", args[1], formatValue(value)) |
| 90 | + return |
| 91 | + default: |
| 92 | + c.helper.ShowConfigHelp() |
| 93 | + return |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func formatValue(value any) string { |
| 98 | + switch v := value.(type) { |
| 99 | + case string: |
| 100 | + return v |
| 101 | + case bool: |
| 102 | + return strconv.FormatBool(v) |
| 103 | + case int, int8, int16, int32, int64: |
| 104 | + return fmt.Sprintf("%d", v) |
| 105 | + case uint, uint8, uint16, uint32, uint64: |
| 106 | + return fmt.Sprintf("%d", v) |
| 107 | + case float32, float64: |
| 108 | + return fmt.Sprintf("%g", v) |
| 109 | + case map[string]any: |
| 110 | + return fmt.Sprintf("%v", v) |
| 111 | + default: |
| 112 | + return fmt.Sprintf("%v", v) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func parseValue(value string) any { |
| 117 | + if b, err := strconv.ParseBool(value); err == nil { |
| 118 | + return b |
| 119 | + } |
| 120 | + if i, err := strconv.ParseInt(value, 10, 64); err == nil { |
| 121 | + return i |
| 122 | + } |
| 123 | + if f, err := strconv.ParseFloat(value, 64); err == nil { |
| 124 | + return f |
| 125 | + } |
| 126 | + return value |
| 127 | +} |
0 commit comments