Skip to content

Commit

Permalink
Fix command line argument handling for slice configuration parameters (
Browse files Browse the repository at this point in the history
…#299)

* Fix command line argument handling for slice configuration parameters (comma separated list).

* Update READMEs.

* Add unset value

* extend waiting

* don't need export unsetDefault

Co-authored-by: Thomas <[email protected]>
Co-authored-by: xiantang <[email protected]>
  • Loading branch information
3 people authored Jul 2, 2022
1 parent 2ffc212 commit b878e3c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README-zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Air 是为 Go 应用开发设计的另外一个热重载的命令行工具。只

`air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api"`

对于以列表形式输入的参数,使用逗号来分隔项目:

`air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api" --build.exclude_dir "templates,build"`

## 安装

### 推荐使用 install.sh
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ if you just want to config build command and run command, you can use like follo

`air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api"`

use a comma to separate items for arguments that take a list as input:

`air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api" --build.exclude_dir "templates,build"`

## Installation

### Prefer install.sh
Expand Down
2 changes: 1 addition & 1 deletion runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (c *Config) rel(path string) string {
// WithArgs returns a new config with the given arguments added to the configuration.
func (c *Config) WithArgs(args map[string]TomlInfo) {
for _, value := range args {
if value.Value != nil && *value.Value != "" {
if value.Value != nil && *value.Value != unsetDefault {
v := reflect.ValueOf(c)
setValue2Struct(v, value.fieldPath, *value.Value)
}
Expand Down
4 changes: 2 additions & 2 deletions runner/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func TestRebuildWhenRunCmdUsingDLV(t *testing.T) {
go func() {
engine.Run()
}()
if err := waitingPortReady(t, port, time.Second*20); err != nil {
if err := waitingPortReady(t, port, time.Second*40); err != nil {
t.Fatalf("Should not be fail: %s.", err)
}

Expand All @@ -588,7 +588,7 @@ func TestRebuildWhenRunCmdUsingDLV(t *testing.T) {
}
t.Logf("connection refused")
time.Sleep(time.Second * 2)
err = waitingPortReady(t, port, time.Second*20)
err = waitingPortReady(t, port, time.Second*40)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand Down
4 changes: 3 additions & 1 deletion runner/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"flag"
)

const unsetDefault = "DEFAULT"

// ParseConfigFlag parse toml information for flag
func ParseConfigFlag(f *flag.FlagSet) map[string]TomlInfo {
c := Config{}
m := flatConfig(c)
for k, v := range m {
f.StringVar(v.Value, k, "", "")
f.StringVar(v.Value, k, unsetDefault, "")
}
return m
}
13 changes: 11 additions & 2 deletions runner/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,18 @@ func TestConfigRuntimeArgs(t *testing.T) {
},
{
name: "check exclude_regex",
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)
},
},
{
name: "check exclude_regex with empty string",
args: []string{"--build.exclude_regex", ""},
check: func(t *testing.T, conf *Config) {
assert.Equal(t, []string{}, conf.Build.ExcludeRegex)
t.Logf("%+v", conf.Build.ExcludeDir)
assert.NotEqual(t, []string{}, conf.Build.ExcludeDir)
},
},
}
Expand Down
10 changes: 8 additions & 2 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,8 +300,10 @@ 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)))
if len(value) == 0 {
field.Set(reflect.ValueOf([]string{}))
} else {
field.Set(reflect.ValueOf(strings.Split(value, sliceCmdArgSeparator)))
}
case reflect.Int64:
i, _ := strconv.ParseInt(value, 10, 64)
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)
}

0 comments on commit b878e3c

Please sign in to comment.