From 8f537b49b3d59784acc3b5ae1030b17d1fc9124c Mon Sep 17 00:00:00 2001 From: silenceper Date: Thu, 25 Apr 2019 00:14:23 +0800 Subject: [PATCH 1/4] =?UTF-8?q?1=E3=80=81fix=20#9=EF=BC=8Cadd=20`disable?= =?UTF-8?q?=5Frun`=20config=202=E3=80=81add=20example=203=E3=80=81add=20ve?= =?UTF-8?q?rsion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++++ config.go | 2 ++ gowatch.yml => example/gowatch.yml | 15 ++++++++++++--- example/main.go | 7 +++++++ gowatch.go | 4 +++- main.go | 21 ++++++++++++++------- version.go | 9 +++++++++ 7 files changed, 52 insertions(+), 11 deletions(-) rename gowatch.yml => example/gowatch.yml (65%) create mode 100644 example/main.go create mode 100644 version.go diff --git a/README.md b/README.md index e664b4e..fa9f6c4 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ go get github.com/silenceper/gowatch - -o : 非必须,指定build的目标文件路径 - -p : 非必须,指定需要build的package(也可以是单个文件) +- -args: 非必须,指定程序运行时参数,例如:-args='-host=:8080,-name=demo' +- -v: 非必须,显示gowatch版本信息 例子: @@ -59,6 +61,9 @@ build_pkg: "" # build tags build_tags: "" +# 是否禁止自动运行 +disable_run: false + ``` diff --git a/config.go b/config.go index 36b893b..68d5883 100644 --- a/config.go +++ b/config.go @@ -31,6 +31,8 @@ type config struct { BuildPkg string `yaml:"build_pkg"` //在go build 时期接收的-tags参数 BuildTags string `yaml:"build_tags"` + //程序是否自动运行 + DisableRun bool `yaml:"disable_run"` } func parseConfig() *config { diff --git a/gowatch.yml b/example/gowatch.yml similarity index 65% rename from gowatch.yml rename to example/gowatch.yml index 15f8511..2930917 100644 --- a/gowatch.yml +++ b/example/gowatch.yml @@ -1,16 +1,25 @@ # gowatch.yml 配置示例 # -#appname: "test" -#go_install: true +# 程序名称 +appname: "example" + +# Output +output: ./example + #watch_exts: # - .yml + #cmd_args: # - arg1=val1 + #envs: # - a=b + #vendor_watch: false #excluded_paths: # - path -# + #build_tags: "" + +disable_run: false diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..c048119 --- /dev/null +++ b/example/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("hello world") +} diff --git a/gowatch.go b/gowatch.go index c960777..9ccf985 100644 --- a/gowatch.go +++ b/gowatch.go @@ -134,7 +134,9 @@ func Autobuild(files []string) { return } log.Infof("Build was successful\n") - Restart(cfg.Output) + if !cfg.DisableRun { + Restart(cfg.Output) + } } func Kill() { diff --git a/main.go b/main.go index c42d0d7..148ee23 100644 --- a/main.go +++ b/main.go @@ -9,12 +9,13 @@ import ( ) var ( - cfg *config - currpath string - exit chan bool - output string - buildPkg string - cmdArgs string + cfg *config + currpath string + exit chan bool + output string + buildPkg string + cmdArgs string + showVersion bool started chan bool ) @@ -23,6 +24,7 @@ func init() { flag.StringVar(&output, "o", "", "go build output") flag.StringVar(&buildPkg, "p", "", "go build packages") flag.StringVar(&cmdArgs, "args", "", "app run args,separated by commas. like: -args='-host=:8080,-name=demo'") + flag.BoolVar(&showVersion, "v", false, "show version") } var ignoredFilesRegExps = []string{ @@ -34,8 +36,13 @@ var ignoredFilesRegExps = []string{ func main() { flag.Parse() - cfg = parseConfig() + if showVersion { + printVersion() + os.Exit(0) + } + + cfg = parseConfig() currpath, _ = os.Getwd() if cfg.AppName == "" { //app名默认取目录名 diff --git a/version.go b/version.go new file mode 100644 index 0000000..5bfe927 --- /dev/null +++ b/version.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +const version = "1.0" + +func printVersion() { + fmt.Println(version) +} From 302329d208262e9d2ebc9c1ed62c184c7f11b204 Mon Sep 17 00:00:00 2001 From: silenceper Date: Thu, 25 Apr 2019 00:43:12 +0800 Subject: [PATCH 2/4] add .travis.yml --- .travis.yml | 16 ++++++++++++++++ gowatch.go | 5 +++++ 2 files changed, 21 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e446da6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +language: go + +go: + - 1.11.x + - 1.10.x + - 1.9.x + - 1.8.x + +before_script: + - GO_FILES=$(find . -iname '*.go' -type f | grep -v /vendor/) + - go get github.com/golang/lint/golint + +script: + - go test -v -race ./... + - go vet ./... + - golint -set_exit_status $(go list ./...) diff --git a/gowatch.go b/gowatch.go index 9ccf985..073b733 100644 --- a/gowatch.go +++ b/gowatch.go @@ -22,6 +22,7 @@ var ( scheduleTime time.Time ) +//NewWatcher new watcher func NewWatcher(paths []string, files []string) { watcher, err := fsnotify.NewWatcher() if err != nil { @@ -104,6 +105,7 @@ func getFileModTime(path string) int64 { return fi.ModTime().Unix() } +//Autobuild auto build func Autobuild(files []string) { state.Lock() defer state.Unlock() @@ -139,6 +141,7 @@ func Autobuild(files []string) { } } +//kill kill process func Kill() { defer func() { if e := recover(); e != nil { @@ -153,12 +156,14 @@ func Kill() { } } +//Restart restart app func Restart(appname string) { //log.Debugf("kill running process") Kill() go Start(appname) } +//Start start app func Start(appname string) { log.Infof("Restarting %s ...\n", appname) if strings.Index(appname, "./") == -1 { From 8e0389330547d038fbba9f0e4c00c16c78e84da2 Mon Sep 17 00:00:00 2001 From: silenceper Date: Thu, 25 Apr 2019 02:41:31 +0800 Subject: [PATCH 3/4] fix golint --- gowatch.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/gowatch.go b/gowatch.go index 073b733..0b918b0 100644 --- a/gowatch.go +++ b/gowatch.go @@ -141,7 +141,7 @@ func Autobuild(files []string) { } } -//kill kill process +//Kill kill process func Kill() { defer func() { if e := recover(); e != nil { @@ -191,9 +191,8 @@ func shouldIgnoreFile(filename string) bool { } if r.MatchString(filename) { return true - } else { - continue } + continue } return false } From c7a2539c627f49e12f274010ab23227a74886592 Mon Sep 17 00:00:00 2001 From: silenceper Date: Thu, 25 Apr 2019 02:45:55 +0800 Subject: [PATCH 4/4] update .travis.yml:remove Go: 1.8.x --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e446da6..082668e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,6 @@ go: - 1.11.x - 1.10.x - 1.9.x - - 1.8.x before_script: - GO_FILES=$(find . -iname '*.go' -type f | grep -v /vendor/)