Skip to content

Commit

Permalink
config: Refactor to avoid duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
cfergeau authored and anjannath committed May 29, 2023
1 parent e3d423e commit cbbe913
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions pkg/crc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func (c *Config) validate(key string, value interface{}) error {
return nil
}

func (c *Config) revalidateSettingsValue(key string) error {
if err := c.validate(key, c.Get(key)); err != nil {
if _, err := c.Unset(key); err != nil {
return err
}
}

return nil
}

// Set sets the value for a given config key
func (c *Config) Set(key string, value interface{}) (string, error) {
setting, ok := c.settingsByName[key]
Expand Down Expand Up @@ -102,21 +112,15 @@ func (c *Config) Set(key string, value interface{}) (string, error) {
return "", fmt.Errorf(invalidType, value, key)
}

// Preset is mapped with `memory`, `cpus` and `bundle` and
// we want to make sure if cpu or memory is less for a preset
// then default is set automatic.
// The `memory` and `cpus` values are preset-dependent.
// When they are lower than the preset requirements, this code
// automatically resets them to their default value.
if setting.Name == Preset {
mem := c.Get(Memory)
if err := c.validate(Memory, mem); err != nil {
if _, err := c.Unset(Memory); err != nil {
return "", err
}
if err := c.revalidateSettingsValue(Memory); err != nil {
return "", err
}
cpu := c.Get(CPUs)
if err := c.validate(CPUs, cpu); err != nil {
if _, err := c.Unset(CPUs); err != nil {
return "", err
}
if err := c.revalidateSettingsValue(CPUs); err != nil {
return "", err
}
}

Expand Down

0 comments on commit cbbe913

Please sign in to comment.