Skip to content

Commit f9a0a4a

Browse files
committed
Config: Unset the default config value
This PR make sure that if user try to set a default value for a config property then it just unset that property and default value is used. It will also allow not to have default value in the viper config file. Also add `UpdateDefaults` to set and get config handler so API can also make use of it. Without this Patch ``` $ crc config set preset microshift $ crc config set cpus 2 $ crc config set preset openshift $ cat ~/.crc/crc.json { "cpus": 2, "preset": "openshift" } ``` After this patch `~/.crc/crc.json` going to be empty and openshift preset is going to use default `cpus` as `4`.
1 parent 31f0f27 commit f9a0a4a

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

pkg/crc/api/handlers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func (h *Handler) SetConfig(c *context) error {
191191
if len(multiError.Errors) != 0 {
192192
return multiError
193193
}
194+
crcConfig.UpdateDefaults(h.Config)
194195
return c.JSON(http.StatusOK, client.SetOrUnsetConfigResult{
195196
Properties: successProps,
196197
})
@@ -219,6 +220,7 @@ func (h *Handler) UnsetConfig(c *context) error {
219220
if len(multiError.Errors) != 0 {
220221
return multiError
221222
}
223+
crcConfig.UpdateDefaults(h.Config)
222224
return c.JSON(http.StatusOK, client.SetOrUnsetConfigResult{
223225
Properties: successProps,
224226
})

pkg/crc/config/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ func (c *Config) Set(key string, value interface{}) (string, error) {
9595
return "", fmt.Errorf(invalidType, value, key)
9696
}
9797

98+
// Make sure if user try to set same value which
99+
// is default then just unset the value which
100+
// anyway make it default and don't update it
101+
// ~/.crc/crc.json (viper config) file.
102+
if setting.defaultValue == castValue {
103+
if _, err := c.Unset(key); err != nil {
104+
return "", err
105+
}
106+
return "", nil
107+
}
108+
98109
if setting.isSecret {
99110
if err := c.secretStorage.Set(key, castValue); err != nil {
100111
return "", err

pkg/crc/config/viper_config_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,14 @@ func TestViperConfigLoadDefaultValue(t *testing.T) {
115115
Value: 4,
116116
IsDefault: true,
117117
}, config.Get(cpus))
118-
119118
_, err = config.Set(cpus, 4)
120119
assert.NoError(t, err)
121120

122121
bin, err := os.ReadFile(configFile)
123122
assert.NoError(t, err)
124-
assert.JSONEq(t, `{"cpus":4}`, string(bin))
123+
// Setting default value will not update
124+
// write to the config so expected would be {}
125+
assert.JSONEq(t, `{}`, string(bin))
125126

126127
assert.Equal(t, SettingValue{
127128
Value: 4,

0 commit comments

Comments
 (0)