From 3e692804f20193a83985bf8f506632c698b1d32b Mon Sep 17 00:00:00 2001 From: Makis Maropoulos Date: Fri, 24 Jun 2016 05:17:22 +0300 Subject: [PATCH] Improve the iris run command as requested here https://github.com/kataras/iris/issues/192 --- HISTORY.md | 2 ++ iris/README.md | 2 +- iris/main.go | 6 +++++- iris/run.go | 26 +++++++++++++++++++++++--- utils/file.go | 13 ++++++------- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 75e13ec8..e712c3ee 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -27,6 +27,8 @@ Underline changes, libraries used by iris' base code: - [Fix or disable colors in iris run](https://github.com/kataras/iris/issues/217). +Improvements to the `iris run` **command**, as requested [here](https://github.com/kataras/iris/issues/192). + [Book](https://kataras.gitbooks.io/iris/content/) and [examples](https://github.com/iris-contrib/examples) are **updated** also. ## 3.0.0-rc.1 -> 3.0.0-rc.2 diff --git a/iris/README.md b/iris/README.md index e3fea752..cc9bd4e2 100644 --- a/iris/README.md +++ b/iris/README.md @@ -7,7 +7,7 @@ This package is the command line tool for [../](https://github.com/kataras/iris [![Iris installed screen](https://raw.githubusercontent.com/iris-contrib/website/gh-pages/assets/iris_cli_screen2.png)](https://raw.githubusercontent.com/iris-contrib/website/gh-pages/assets/iris_cli_screen2.png) ## Install -Current version: 0.0.6 +Current version: 0.0.7 ```sh go get -u github.com/kataras/iris/iris diff --git a/iris/main.go b/iris/main.go index 3fa99430..693b1351 100644 --- a/iris/main.go +++ b/iris/main.go @@ -13,6 +13,10 @@ import ( "github.com/kataras/iris/logger" ) +const ( + Version = "0.0.7" +) + var ( app *cli.App printer *logger.Logger @@ -33,7 +37,7 @@ func init() { defaultInstallDir := workingDir[strings.LastIndexByte(workingDir, os.PathSeparator)+1:] // init the cli app - app = cli.NewApp("iris", "Command line tool for Iris web framework", "0.0.6") + app = cli.NewApp("iris", "Command line tool for Iris web framework", Version) // version command app.Command(cli.Command("version", "\t prints your iris version").Action(func(cli.Flags) error { app.Printf("%s", iris.Version); return nil })) diff --git a/iris/run.go b/iris/run.go index abf82eb4..0db696e6 100644 --- a/iris/run.go +++ b/iris/run.go @@ -1,6 +1,7 @@ package main import ( + "io/ioutil" "os" "path/filepath" "runtime" @@ -38,6 +39,7 @@ func build(sourcepath string) error { func run(executablePath string, stdout bool) (*utils.Cmd, error) { runCmd := utils.CommandBuilder("." + utils.PathSeparator + executablePath) runCmd.Dir = workingDir + runCmd.Stderr = os.Stderr if stdout { runCmd.Stdout = os.Stdout } @@ -87,10 +89,25 @@ func runAndWatch(flags cli.Flags) error { } } - // here(below), we don't return the error because the -help command doesn't help the user for these errors. + + subfiles, err := ioutil.ReadDir(workingDir) + if err != nil { + printer.Dangerf(err.Error()) + return err + } + var paths []string + paths = append(paths, workingDir) + for _, subfile := range subfiles { + if subfile.IsDir() { + if abspath, err := filepath.Abs(workingDir + utils.PathSeparator + subfile.Name()); err == nil { + paths = append(paths, abspath) + } + + } + } // run the file watcher before all, because the user maybe has a go syntax error before the first run - utils.WatchDirectoryChanges(workingDir, func(fname string) { + utils.WatchDirectoryChanges(paths, func(fname string) { //remove the working dir from the fname path, printer should only print the relative changed file ( from the project's path) fname = fname[len(workingDir)+1:] @@ -101,14 +118,17 @@ func runAndWatch(flags cli.Flags) error { }, printer) if err := build(programPath); err != nil { + printer.Dangerf(err.Error()) return err } runCmd, err := run(executablePath, true) + if err != nil { + printer.Dangerf(err.Error()) return err } - + // here(below), we don't return the error because the -help command doesn't help the user for these errors. defer func() { printer.Dangerf("") printer.Panic(errUnexpected) diff --git a/utils/file.go b/utils/file.go index ad5d42a5..75bac032 100644 --- a/utils/file.go +++ b/utils/file.go @@ -340,7 +340,7 @@ func GetParentDir(targetDirectory string) string { */ // WatchDirectoryChanges watches a directory and fires the callback with the changed name, receives a logger just to print with red letters any errors, no need for second callback. -func WatchDirectoryChanges(rootPath string, evt func(filename string), logger *logger.Logger) { +func WatchDirectoryChanges(paths []string, evt func(filename string), logger *logger.Logger) { isWindows := runtime.GOOS == "windows" watcher, werr := fsnotify.NewWatcher() if werr != nil { @@ -357,7 +357,7 @@ func WatchDirectoryChanges(rootPath string, evt func(filename string), logger *l if event.Op&fsnotify.Write == fsnotify.Write { //this is received two times, the last time is the real changed file, so i++ - if i%2 == 0 || !isWindows { // this 'hack' works for windows but I dont know if works for linux too, we can wait for issue reports here. + if i%2 == 0 || !isWindows { // this 'hack' works for windows & linux but I dont know if works for osx too, we can wait for issue reports here. if time.Now().After(lastChange.Add(time.Duration(1) * time.Second)) { lastChange = time.Now() evt(event.Name) @@ -370,11 +370,10 @@ func WatchDirectoryChanges(rootPath string, evt func(filename string), logger *l } } }() - - werr = watcher.Add(rootPath) - if werr != nil { - logger.Dangerf(werr.Error()) - + for _, p := range paths { + if werr = watcher.Add(p); werr != nil { + logger.Dangerf(werr.Error()) + } } }