From 02672bed40884a85509b094ec1926fab8f8015f9 Mon Sep 17 00:00:00 2001 From: Vlad Velici Date: Thu, 10 Dec 2015 14:21:21 +0000 Subject: [PATCH] Add command line flag "--ext" (short "-e") to specify a list of file extensions to watch for changes. Addresses issue https://github.com/codegangsta/gin/issues/44. Restarting on template change can be achieved using flags "-e .go -e .html". --- main.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index e9310a0..b43f5bd 100644 --- a/main.go +++ b/main.go @@ -45,6 +45,11 @@ func main() { Value: "gin-bin", Usage: "name of generated binary file", }, + cli.StringSliceFlag{ + Name: "ext,e", + Usage: "Use ext to specify the file extensions to listen to.", + Value: &cli.StringSlice{".go"}, + }, cli.StringFlag{ Name: "path,t", Value: ".", @@ -116,7 +121,7 @@ func MainAction(c *cli.Context) { build(builder, runner, logger) // scan for changes - scanChanges(c.GlobalString("path"), func(path string) { + scanChanges(c.GlobalString("path"), c.GlobalStringSlice("ext"), func(path string) { runner.Kill() build(builder, runner, logger) }) @@ -157,7 +162,16 @@ func build(builder gin.Builder, runner gin.Runner, logger *log.Logger) { type scanCallback func(path string) -func scanChanges(watchPath string, cb scanCallback) { +func in(str string, arr []string) bool { + for _, v := range arr { + if v == str { + return true + } + } + return false +} + +func scanChanges(watchPath string, ext []string, cb scanCallback) { for { filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error { if path == ".git" { @@ -169,7 +183,7 @@ func scanChanges(watchPath string, cb scanCallback) { return nil } - if filepath.Ext(path) == ".go" && info.ModTime().After(startTime) { + if in(filepath.Ext(path), ext) && info.ModTime().After(startTime) { cb(path) startTime = time.Now() return errors.New("done")