|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "github.com/steveyegge/wasteland/internal/federation" |
| 9 | +) |
| 10 | + |
| 11 | +func newConfigCmd(stdout, stderr io.Writer) *cobra.Command { |
| 12 | + cmd := &cobra.Command{ |
| 13 | + Use: "config", |
| 14 | + Short: "Get or set wasteland configuration", |
| 15 | + Long: `View or modify wasteland configuration settings. |
| 16 | +
|
| 17 | +Use 'wl config get <key>' to read a setting. |
| 18 | +Use 'wl config set <key> <value>' to change a setting. |
| 19 | +
|
| 20 | +Supported keys: |
| 21 | + mode Workflow mode: wild-west (default) or pr`, |
| 22 | + Args: cobra.NoArgs, |
| 23 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 24 | + return cmd.Help() |
| 25 | + }, |
| 26 | + } |
| 27 | + |
| 28 | + cmd.AddCommand( |
| 29 | + newConfigGetCmd(stdout, stderr), |
| 30 | + newConfigSetCmd(stdout, stderr), |
| 31 | + ) |
| 32 | + |
| 33 | + return cmd |
| 34 | +} |
| 35 | + |
| 36 | +func newConfigGetCmd(stdout, stderr io.Writer) *cobra.Command { |
| 37 | + return &cobra.Command{ |
| 38 | + Use: "get <key>", |
| 39 | + Short: "Get a configuration value", |
| 40 | + Args: cobra.ExactArgs(1), |
| 41 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 42 | + return runConfigGet(cmd, stdout, stderr, args[0]) |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func newConfigSetCmd(stdout, stderr io.Writer) *cobra.Command { |
| 48 | + return &cobra.Command{ |
| 49 | + Use: "set <key> <value>", |
| 50 | + Short: "Set a configuration value", |
| 51 | + Args: cobra.ExactArgs(2), |
| 52 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 53 | + return runConfigSet(cmd, stdout, stderr, args[0], args[1]) |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// validConfigKeys lists the keys that can be read/written via wl config. |
| 59 | +var validConfigKeys = map[string]bool{ |
| 60 | + "mode": true, |
| 61 | +} |
| 62 | + |
| 63 | +func runConfigGet(cmd *cobra.Command, stdout, _ io.Writer, key string) error { |
| 64 | + if !validConfigKeys[key] { |
| 65 | + return fmt.Errorf("unknown config key %q (supported: mode)", key) |
| 66 | + } |
| 67 | + |
| 68 | + cfg, err := resolveWasteland(cmd) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("loading wasteland config: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + if key == "mode" { |
| 74 | + fmt.Fprintln(stdout, cfg.ResolveMode()) |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func runConfigSet(cmd *cobra.Command, stdout, _ io.Writer, key, value string) error { |
| 80 | + if !validConfigKeys[key] { |
| 81 | + return fmt.Errorf("unknown config key %q (supported: mode)", key) |
| 82 | + } |
| 83 | + |
| 84 | + if key == "mode" { |
| 85 | + if err := validateMode(value); err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + explicit, _ := cmd.Flags().GetString("wasteland") |
| 91 | + store := federation.NewConfigStore() |
| 92 | + cfg, err := federation.ResolveConfig(store, explicit) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("loading wasteland config: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + if key == "mode" { |
| 98 | + cfg.Mode = value |
| 99 | + } |
| 100 | + |
| 101 | + if err := store.Save(cfg); err != nil { |
| 102 | + return fmt.Errorf("saving wasteland config: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + fmt.Fprintf(stdout, "%s = %s\n", key, value) |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func validateMode(value string) error { |
| 110 | + switch value { |
| 111 | + case federation.ModeWildWest, federation.ModePR: |
| 112 | + return nil |
| 113 | + default: |
| 114 | + return fmt.Errorf("invalid mode %q: must be %q or %q", value, federation.ModeWildWest, federation.ModePR) |
| 115 | + } |
| 116 | +} |
0 commit comments