|
5 | 5 | "reflect"
|
6 | 6 |
|
7 | 7 | "github.com/crc-org/crc/pkg/crc/preset"
|
8 |
| - "github.com/crc-org/crc/pkg/crc/validation" |
9 | 8 | "github.com/spf13/cast"
|
10 | 9 | )
|
11 | 10 |
|
@@ -63,16 +62,23 @@ func (c *Config) AddSetting(name string, defValue interface{}, validationFn Vali
|
63 | 62 | }
|
64 | 63 | }
|
65 | 64 |
|
| 65 | +func (c *Config) validate(key string, value interface{}) error { |
| 66 | + ok, expectedValue := c.settingsByName[key].validationFn(value) |
| 67 | + if !ok { |
| 68 | + return fmt.Errorf(invalidProp, value, key, expectedValue) |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
66 | 73 | // Set sets the value for a given config key
|
67 | 74 | func (c *Config) Set(key string, value interface{}) (string, error) {
|
68 | 75 | setting, ok := c.settingsByName[key]
|
69 | 76 | if !ok {
|
70 | 77 | return "", fmt.Errorf(configPropDoesntExistMsg, key)
|
71 | 78 | }
|
72 | 79 |
|
73 |
| - ok, expectedValue := c.settingsByName[key].validationFn(value) |
74 |
| - if !ok { |
75 |
| - return "", fmt.Errorf(invalidProp, value, key, expectedValue) |
| 80 | + if err := c.validate(key, value); err != nil { |
| 81 | + return "", err |
76 | 82 | }
|
77 | 83 |
|
78 | 84 | var castValue interface{}
|
@@ -100,15 +106,14 @@ func (c *Config) Set(key string, value interface{}) (string, error) {
|
100 | 106 | // we want to make sure if cpu or memory is less for a preset
|
101 | 107 | // then default is set automatic.
|
102 | 108 | if setting.Name == Preset {
|
103 |
| - preset := preset.ParsePreset(value.(string)) |
104 | 109 | mem := c.Get(Memory)
|
105 |
| - if err := validation.ValidateMemory(mem.AsInt(), preset); err != nil { |
| 110 | + if err := c.validate(Memory, mem); err != nil { |
106 | 111 | if _, err := c.Unset(Memory); err != nil {
|
107 | 112 | return "", err
|
108 | 113 | }
|
109 | 114 | }
|
110 | 115 | cpu := c.Get(CPUs)
|
111 |
| - if err := validation.ValidateCPUs(cpu.AsInt(), preset); err != nil { |
| 116 | + if err := c.validate(CPUs, cpu); err != nil { |
112 | 117 | if _, err := c.Unset(CPUs); err != nil {
|
113 | 118 | return "", err
|
114 | 119 | }
|
|
0 commit comments