Skip to content

Commit

Permalink
Use flag default when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
domenipavec committed Apr 7, 2022
1 parent 4bdd090 commit bc7bbd1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (fvp *FlagValuesProvider) ToKeyValues() map[string]interface{} {

func (fvp *FlagValuesProvider) Read(name string) (val interface{}, found bool) {
fl := fvp.flags.Lookup(name)
if fl == nil || !fl.Changed {
if fl == nil {
return nil, false
}

Expand Down
11 changes: 6 additions & 5 deletions internal/pkg/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,19 +331,20 @@ func TestCollectParams(t *testing.T) {

func TestFlagValuesProvider(t *testing.T) {
fl := &pflag.FlagSet{}
fl.StringP("somekey", "s", "", "")
fl.StringP("somekey", "s", "test-default", "")

flagValuesProv := CreateFlagValuesProvider(fl)

_, found := flagValuesProv.Read("somekey")
assert.False(t, found)
val, found := flagValuesProv.Read("somekey")
assert.True(t, found)
assert.Equal(t, "test-default", val)

err := fl.Parse([]string{"--somekey", "someval"})
require.NoError(t, err)

val, found2 := flagValuesProv.Read("somekey")
val2, found2 := flagValuesProv.Read("somekey")
assert.True(t, found2)
assert.Equal(t, "someval", val.(string))
assert.Equal(t, "someval", val2)

actualKeyValues := flagValuesProv.ToKeyValues()
assert.Equal(t, map[string]interface{}{"somekey": "someval"}, actualKeyValues)
Expand Down

0 comments on commit bc7bbd1

Please sign in to comment.