Skip to content

Commit

Permalink
Merge pull request #10 from silenceper/feature/disable-run
Browse files Browse the repository at this point in the history
fix #9,add `disable_run` config
  • Loading branch information
silenceper authored Apr 24, 2019
2 parents 88b1525 + c7a2539 commit 58727ec
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 13 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: go

go:
- 1.11.x
- 1.10.x
- 1.9.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 ./...)
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ go get github.com/silenceper/gowatch

- -o : 非必须,指定build的目标文件路径
- -p : 非必须,指定需要build的package(也可以是单个文件)
- -args: 非必须,指定程序运行时参数,例如:-args='-host=:8080,-name=demo'
- -v: 非必须,显示gowatch版本信息

例子:

Expand Down Expand Up @@ -59,6 +61,9 @@ build_pkg: ""
# build tags
build_tags: ""
# 是否禁止自动运行
disable_run: false
```


Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 12 additions & 3 deletions gowatch.yml → example/gowatch.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("hello world")
}
12 changes: 9 additions & 3 deletions gowatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
scheduleTime time.Time
)

//NewWatcher new watcher
func NewWatcher(paths []string, files []string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -134,9 +136,12 @@ func Autobuild(files []string) {
return
}
log.Infof("Build was successful\n")
Restart(cfg.Output)
if !cfg.DisableRun {
Restart(cfg.Output)
}
}

//Kill kill process
func Kill() {
defer func() {
if e := recover(); e != nil {
Expand All @@ -151,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 {
Expand Down Expand Up @@ -184,9 +191,8 @@ func shouldIgnoreFile(filename string) bool {
}
if r.MatchString(filename) {
return true
} else {
continue
}
continue
}
return false
}
Expand Down
21 changes: 14 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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{
Expand All @@ -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名默认取目录名
Expand Down
9 changes: 9 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "fmt"

const version = "1.0"

func printVersion() {
fmt.Println(version)
}

0 comments on commit 58727ec

Please sign in to comment.