From b0ce1d15d70a651de33d01f2342c4dcd1d8f8535 Mon Sep 17 00:00:00 2001 From: silenceper Date: Fri, 4 Sep 2020 14:13:29 +0800 Subject: [PATCH 1/2] add external cmds --- README.md | 4 ++++ README_ZH_CN.md | 3 +++ config.go | 2 ++ example/gowatch.yml | 3 +++ gowatch.go | 18 +++++++++++++++--- version.go | 2 +- 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3e2d32a..41409d4 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,10 @@ build_pkg: "" # build tags build_tags: "" +# Commands that can be executed before running the app +#external_cmd: +# - swag init + # Whether to prohibit automatic operation disable_run: false diff --git a/README_ZH_CN.md b/README_ZH_CN.md index 79ae41f..1be694b 100644 --- a/README_ZH_CN.md +++ b/README_ZH_CN.md @@ -70,6 +70,9 @@ build_tags: "" # 是否禁止自动运行 disable_run: false +#在执行app执行的命令 ,例如 swag init +#external_cmd: +# - swag init ``` ## 微信公众号 diff --git a/config.go b/config.go index e3d5b76..3ec4778 100644 --- a/config.go +++ b/config.go @@ -19,6 +19,8 @@ type config struct { WatchExts []string `yaml:"watch_exts"` //需要追加监听的目录,默认是当前文件夹, WatchPaths []string `yaml:"watch_paths"` + //运行前额外执行的命令 + ExternalCmd []string `yaml:"external_cmd"` //执行时的额外参数 CmdArgs []string `yaml:"cmd_args"` //构建时的额外参数 diff --git a/example/gowatch.yml b/example/gowatch.yml index 0c34ccc..372e195 100644 --- a/example/gowatch.yml +++ b/example/gowatch.yml @@ -25,4 +25,7 @@ output: ./example #build_tags: "" +#external_cmd: +# - go version + disable_run: false diff --git a/gowatch.go b/gowatch.go index c648475..968b4c5 100644 --- a/gowatch.go +++ b/gowatch.go @@ -174,12 +174,24 @@ func Start(appname string) { appname = "./" + appname } - cmd = exec.Command(appname) + for _, externalCmd := range cfg.ExternalCmd { + log.Infof("Run external cmd '%s'", externalCmd) + cmdArr := strings.Split(externalCmd, " ") + externalCmdExec := exec.Command(cmdArr[0]) + externalCmdExec.Env = append(os.Environ(), cfg.Envs...) + externalCmdExec.Args = cmdArr + externalCmdExec.Stdout = os.Stdout + externalCmdExec.Stderr = os.Stderr + err := externalCmdExec.Run() + if err != nil { + panic(err) + } + } + cmd = exec.Command(appname, cfg.CmdArgs...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - cmd.Args = append([]string{appname}, cfg.CmdArgs...) cmd.Env = append(os.Environ(), cfg.Envs...) - log.Infof("Run %s", strings.Join(cmd.Args, " ")) + log.Infof("Run '%s'", strings.Join(cmd.Args, " ")) go func() { _ = cmd.Run() }() diff --git a/version.go b/version.go index f960901..8ace6e0 100644 --- a/version.go +++ b/version.go @@ -2,7 +2,7 @@ package main import "fmt" -const version = "1.3" +const version = "1.4" func printVersion() { fmt.Println(version) From 25499271f26e3c2b57c2a86395f4737f80e811a0 Mon Sep 17 00:00:00 2001 From: silenceper Date: Fri, 4 Sep 2020 14:16:09 +0800 Subject: [PATCH 2/2] rename external_cmd to external_cmds --- config.go | 2 +- gowatch.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 3ec4778..f4fff63 100644 --- a/config.go +++ b/config.go @@ -20,7 +20,7 @@ type config struct { //需要追加监听的目录,默认是当前文件夹, WatchPaths []string `yaml:"watch_paths"` //运行前额外执行的命令 - ExternalCmd []string `yaml:"external_cmd"` + ExternalCmds []string `yaml:"external_cmds"` //执行时的额外参数 CmdArgs []string `yaml:"cmd_args"` //构建时的额外参数 diff --git a/gowatch.go b/gowatch.go index 968b4c5..ab1717e 100644 --- a/gowatch.go +++ b/gowatch.go @@ -174,7 +174,7 @@ func Start(appname string) { appname = "./" + appname } - for _, externalCmd := range cfg.ExternalCmd { + for _, externalCmd := range cfg.ExternalCmds { log.Infof("Run external cmd '%s'", externalCmd) cmdArr := strings.Split(externalCmd, " ") externalCmdExec := exec.Command(cmdArr[0])