| 60 | } |
| 61 | |
| 62 | func setRun(opts *SetOptions) error { |
| 63 | err := ValidateKey(opts.Key) |
| 64 | if err != nil { |
| 65 | warningIcon := opts.IO.ColorScheme().WarningIcon() |
| 66 | fmt.Fprintf(opts.IO.ErrOut, "%s warning: '%s' is not a known configuration key\n", warningIcon, opts.Key) |
| 67 | } |
| 68 | |
| 69 | err = ValidateValue(opts.Key, opts.Value) |
| 70 | if err != nil { |
| 71 | var invalidValue InvalidValueError |
| 72 | if errors.As(err, &invalidValue) { |
| 73 | var values []string |
| 74 | for _, v := range invalidValue.ValidValues { |
| 75 | values = append(values, fmt.Sprintf("'%s'", v)) |
| 76 | } |
| 77 | return fmt.Errorf("failed to set %q to %q: valid values are %v", opts.Key, opts.Value, strings.Join(values, ", ")) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | opts.Config.Set(opts.Hostname, opts.Key, opts.Value) |
| 82 | |
| 83 | err = opts.Config.Write() |
| 84 | if err != nil { |
| 85 | return fmt.Errorf("failed to write config to disk: %w", err) |
| 86 | } |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | func ValidateKey(key string) error { |
| 91 | for _, configKey := range config.Options { |