Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix command line argument handling for slice configuration parameters #299

Merged
merged 5 commits into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions runner/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func TestConfigRuntimeArgs(t *testing.T) {
},
{
name: "check exclude_regex",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add the test case which is empty slice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm what would you expect in that case, the default values ore empty slice? It seems currently it keeps the default values (I didn't think of this case before), but I'd rather expect to set it to empty.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer set it to empty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I think it isn't that straightforward. Before a value is set there's a check for nil and empty string https:/cosmtrek/air/blob/master/runner/config.go#L324. If I only keep the nil check it would work (with an additional change in util.go to explicitly set an empty slice), but that will override all the default values because arguments that are not explicitly set seem to end up as empty strings instead of nil. Do you think it would be easy to adjust that non-set arguments would be nil instead of empty strings? I don't know the code base well enough yet and I have not so much experience with the flag package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I see correctly it's only possible to define string variable flags with a string as default value (not a pointer, thus impossible to set nil as default value).

See also here https:/cosmtrek/air/blob/master/runner/flag.go#L12.

Copy link
Collaborator

@xiantang xiantang Jul 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your support. I cherry-picked all 3 commits.

args: []string{"--build.exclude_regex", `["_test.go"]`},
args: []string{"--build.exclude_regex", "_test.go,.html"},
check: func(t *testing.T, conf *Config) {
assert.Equal(t, []string{"_test.go"}, conf.Build.ExcludeRegex)
assert.Equal(t, []string{"_test.go", ".html"}, conf.Build.ExcludeRegex)
},
},
}
Expand Down
8 changes: 5 additions & 3 deletions runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"github.com/fsnotify/fsnotify"
)

const (
sliceCmdArgSeparator = ","
)

func (e *Engine) mainLog(format string, v ...interface{}) {
e.logWithLock(func() {
e.logger.main()(format, v...)
Expand Down Expand Up @@ -296,9 +300,7 @@ func setValue2Struct(v reflect.Value, fieldName string, value string) {
case reflect.String:
field.SetString(value)
case reflect.Slice:
if field.Len() == 0 {
field.Set(reflect.Append(field, reflect.ValueOf(value)))
}
field.Set(reflect.ValueOf(strings.Split(value, sliceCmdArgSeparator)))
case reflect.Int64:
i, _ := strconv.ParseInt(value, 10, 64)
field.SetInt(i)
Expand Down
18 changes: 18 additions & 0 deletions runner/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,21 @@ func TestNestStructValue(t *testing.T) {
setValue2Struct(v, "Build.Cmd", "asdasd")
assert.Equal(t, "asdasd", c.Build.Cmd)
}

func TestNestStructArrayValue(t *testing.T) {
c := Config{}
v := reflect.ValueOf(&c)
setValue2Struct(v, "Build.ExcludeDir", "dir1,dir2")
assert.Equal(t, []string{"dir1", "dir2"}, c.Build.ExcludeDir)
}

func TestNestStructArrayValueOverride(t *testing.T) {
c := Config{
Build: cfgBuild{
ExcludeDir: []string{"default1", "default2"},
},
}
v := reflect.ValueOf(&c)
setValue2Struct(v, "Build.ExcludeDir", "dir1,dir2")
assert.Equal(t, []string{"dir1", "dir2"}, c.Build.ExcludeDir)
}