Skip to content

Commit

Permalink
Merge pull request #23 from silenceper/f-support-external-cmd
Browse files Browse the repository at this point in the history
support external cmd
  • Loading branch information
silenceper authored Sep 4, 2020
2 parents 8e473a9 + 2549927 commit 55bc440
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions README_ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ build_tags: ""
# 是否禁止自动运行
disable_run: false
#在执行app执行的命令 ,例如 swag init
#external_cmd:
# - swag init
```

## 微信公众号
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
//构建时的额外参数
Expand Down
3 changes: 3 additions & 0 deletions example/gowatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ output: ./example

#build_tags: ""

#external_cmd:
# - go version

disable_run: false
18 changes: 15 additions & 3 deletions gowatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}()
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import "fmt"

const version = "1.3"
const version = "1.4"

func printVersion() {
fmt.Println(version)
Expand Down

0 comments on commit 55bc440

Please sign in to comment.