Skip to content

Commit cbbe913

Browse files
cfergeauanjannath
authored andcommitted
config: Refactor to avoid duplicated code
1 parent e3d423e commit cbbe913

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

pkg/crc/config/config.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ func (c *Config) validate(key string, value interface{}) error {
7070
return nil
7171
}
7272

73+
func (c *Config) revalidateSettingsValue(key string) error {
74+
if err := c.validate(key, c.Get(key)); err != nil {
75+
if _, err := c.Unset(key); err != nil {
76+
return err
77+
}
78+
}
79+
80+
return nil
81+
}
82+
7383
// Set sets the value for a given config key
7484
func (c *Config) Set(key string, value interface{}) (string, error) {
7585
setting, ok := c.settingsByName[key]
@@ -102,21 +112,15 @@ func (c *Config) Set(key string, value interface{}) (string, error) {
102112
return "", fmt.Errorf(invalidType, value, key)
103113
}
104114

105-
// Preset is mapped with `memory`, `cpus` and `bundle` and
106-
// we want to make sure if cpu or memory is less for a preset
107-
// then default is set automatic.
115+
// The `memory` and `cpus` values are preset-dependent.
116+
// When they are lower than the preset requirements, this code
117+
// automatically resets them to their default value.
108118
if setting.Name == Preset {
109-
mem := c.Get(Memory)
110-
if err := c.validate(Memory, mem); err != nil {
111-
if _, err := c.Unset(Memory); err != nil {
112-
return "", err
113-
}
119+
if err := c.revalidateSettingsValue(Memory); err != nil {
120+
return "", err
114121
}
115-
cpu := c.Get(CPUs)
116-
if err := c.validate(CPUs, cpu); err != nil {
117-
if _, err := c.Unset(CPUs); err != nil {
118-
return "", err
119-
}
122+
if err := c.revalidateSettingsValue(CPUs); err != nil {
123+
return "", err
120124
}
121125
}
122126

0 commit comments

Comments
 (0)