From 6d9b722a5360a926ae9b034eb1c5f17ebf5aa4ab Mon Sep 17 00:00:00 2001 From: silenceper Date: Sat, 27 Jul 2019 14:32:13 +0800 Subject: [PATCH] add build_args --- README.md | 3 +++ config.go | 2 ++ example/gowatch.yml | 3 +++ gowatch.go | 11 +++++++++-- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e71a67d..45c88c9 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,9 @@ watch_paths: # 在执行命令时,需要增加的其他参数 cmd_args: - arg1=val1 +# 在构建命令时,需要增加的其他参数 +build_args: + - -race # 需要增加环境变量,默认已加载当前环境变量 envs: - a=b diff --git a/config.go b/config.go index 68d5883..e3d5b76 100644 --- a/config.go +++ b/config.go @@ -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 目录下的文件是否也监听 diff --git a/example/gowatch.yml b/example/gowatch.yml index 2930917..0c34ccc 100644 --- a/example/gowatch.yml +++ b/example/gowatch.yml @@ -12,6 +12,9 @@ output: ./example #cmd_args: # - arg1=val1 +#build_args: +# - -race + #envs: # - a=b diff --git a/gowatch.go b/gowatch.go index 0b918b0..1027f89 100644 --- a/gowatch.go +++ b/gowatch.go @@ -112,7 +112,10 @@ 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" @@ -120,6 +123,7 @@ func Autobuild(files []string) { args := []string{"build"} args = append(args, "-o", cfg.Output) + args = append(args, cfg.BuildArgs...) if cfg.BuildTags != "" { args = append(args, "-tags", cfg.BuildTags) } @@ -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") @@ -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 }