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..f4fff63 100644 --- a/config.go +++ b/config.go @@ -19,6 +19,8 @@ type config struct { WatchExts []string `yaml:"watch_exts"` //需要追加监听的目录,默认是当前文件夹, WatchPaths []string `yaml:"watch_paths"` + //运行前额外执行的命令 + ExternalCmds []string `yaml:"external_cmds"` //执行时的额外参数 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..ab1717e 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.ExternalCmds { + 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)