Skip to content

Commit

Permalink
add build_args
Browse files Browse the repository at this point in the history
  • Loading branch information
silenceper committed Jul 27, 2019
1 parent 6b2f9f1 commit 6d9b722
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ watch_paths:
# 在执行命令时,需要增加的其他参数
cmd_args:
- arg1=val1
# 在构建命令时,需要增加的其他参数
build_args:
- -race
# 需要增加环境变量,默认已加载当前环境变量
envs:
- a=b
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type config struct {
WatchPaths []string `yaml:"watch_paths"`
//执行时的额外参数
CmdArgs []string `yaml:"cmd_args"`
//构建时的额外参数
BuildArgs []string `yaml:"build_args"`
//执行时追加的环境变量
Envs []string `yaml:"envs"`
//vendor 目录下的文件是否也监听
Expand Down
3 changes: 3 additions & 0 deletions example/gowatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ output: ./example
#cmd_args:
# - arg1=val1

#build_args:
# - -race

#envs:
# - a=b

Expand Down
11 changes: 9 additions & 2 deletions gowatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,18 @@ func Autobuild(files []string) {

log.Infof("Start building...\n")

os.Chdir(currpath)
if err := os.Chdir(currpath); err != nil {
log.Errorf("Chdir Error: %+v\n", err)
return
}

cmdName := "go"

var err error

args := []string{"build"}
args = append(args, "-o", cfg.Output)
args = append(args, cfg.BuildArgs...)
if cfg.BuildTags != "" {
args = append(args, "-tags", cfg.BuildTags)
}
Expand All @@ -129,10 +133,12 @@ func Autobuild(files []string) {
bcmd.Env = append(os.Environ(), "GOGC=off")
bcmd.Stdout = os.Stdout
bcmd.Stderr = os.Stderr
log.Infof("Build Args: %s %s", cmdName, strings.Join(args, " "))
err = bcmd.Run()

if err != nil {
log.Errorf("============== Build failed ===================\n")
log.Errorf("%+v\n", err)
return
}
log.Infof("Build was successful\n")
Expand Down Expand Up @@ -175,8 +181,9 @@ func Start(appname string) {
cmd.Stderr = os.Stderr
cmd.Args = append([]string{appname}, cfg.CmdArgs...)
cmd.Env = append(os.Environ(), cfg.Envs...)

log.Infof("Run %s %s", appname, strings.Join(cmd.Args, " "))
go cmd.Run()

log.Infof("%s is running...\n", appname)
started <- true
}
Expand Down

1 comment on commit 6d9b722

@silenceper
Copy link
Owner Author

Choose a reason for hiding this comment

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

fix #11

Please sign in to comment.