Skip to content

Commit

Permalink
Allow empty value for config set (vmware-tanzu#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
chandrareddyp authored Aug 2, 2023
1 parent ce40415 commit 8a673a1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
9 changes: 2 additions & 7 deletions config/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,12 @@ func SetEnv(key, value string) (err error) {
return err
}

//nolint:dupl
func setEnv(node *yaml.Node, key, value string) (persist bool, err error) {
// check if key is empty
if key == "" {
return false, errors.New("key cannot be empty")
}

// check if value is empty
if value == "" {
return false, errors.New("value cannot be empty")
}

// find env node
keys := []nodeutils.Key{
{Name: KeyClientOptions, Type: yaml.MappingNode},
Expand All @@ -160,7 +154,8 @@ func setEnv(node *yaml.Node, key, value string) (persist bool, err error) {
}

// add or update the envs map per specified key value pair
if len(envs) == 0 || envs[key] != value {
// value could be empty string
if len(envs) == 0 || envs[key] != value || value == "" {
envs[key] = value
persist = true
}
Expand Down
23 changes: 13 additions & 10 deletions config/envs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func TestGetEnv(t *testing.T) {
tests := []struct {
name string
in *configtypes.ClientConfig
out string
getEnvInput string
getEnvOutput string
errStr string
errStrForInput string
}{
Expand All @@ -76,7 +77,8 @@ func TestGetEnv(t *testing.T) {
},
},
},
out: "test",
getEnvInput: "test",
getEnvOutput: "test",
},
{
name: "get options with empty key",
Expand All @@ -87,8 +89,9 @@ func TestGetEnv(t *testing.T) {
},
},
},
out: "",
errStr: "key cannot be empty",
getEnvInput: "",
getEnvOutput: "",
errStr: "key cannot be empty",
},
{
name: "store options with empty key",
Expand All @@ -99,7 +102,8 @@ func TestGetEnv(t *testing.T) {
},
},
},
out: "",
getEnvInput: "",
getEnvOutput: "",
errStr: "key cannot be empty",
errStrForInput: "key cannot be empty",
},
Expand All @@ -112,9 +116,8 @@ func TestGetEnv(t *testing.T) {
},
},
},
out: "test-empty-val",
errStr: "not found",
errStrForInput: "value cannot be empty",
getEnvInput: "test-empty-val",
getEnvOutput: "",
},
}
for _, spec := range tests {
Expand All @@ -126,12 +129,12 @@ func TestGetEnv(t *testing.T) {
assert.NoError(t, err)
}

c, err := GetEnv(spec.out)
c, err := GetEnv(spec.getEnvInput)
if spec.errStr != "" {
assert.Equal(t, spec.errStr, err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, spec.out, c)
assert.Equal(t, spec.getEnvOutput, c)
}
})
}
Expand Down
1 change: 0 additions & 1 deletion config/metadata_settings_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func deleteSetting(node *yaml.Node, key string) (err error) {
return nil
}

//nolint:dupl
func setSetting(node *yaml.Node, key, value string) (persist bool, err error) {
// check if key is empty
if key == "" {
Expand Down

0 comments on commit 8a673a1

Please sign in to comment.