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