mirror of
https://github.com/kataras/iris.git
synced 2025-02-09 02:34:55 +01:00
Improve the iris run command as requested here https://github.com/kataras/iris/issues/192
This commit is contained in:
parent
95813bf36b
commit
3e692804f2
|
@ -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).
|
- [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.
|
[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
|
## 3.0.0-rc.1 -> 3.0.0-rc.2
|
||||||
|
|
|
@ -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)
|
[![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
|
## Install
|
||||||
Current version: 0.0.6
|
Current version: 0.0.7
|
||||||
```sh
|
```sh
|
||||||
|
|
||||||
go get -u github.com/kataras/iris/iris
|
go get -u github.com/kataras/iris/iris
|
||||||
|
|
|
@ -13,6 +13,10 @@ import (
|
||||||
"github.com/kataras/iris/logger"
|
"github.com/kataras/iris/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Version = "0.0.7"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
app *cli.App
|
app *cli.App
|
||||||
printer *logger.Logger
|
printer *logger.Logger
|
||||||
|
@ -33,7 +37,7 @@ func init() {
|
||||||
defaultInstallDir := workingDir[strings.LastIndexByte(workingDir, os.PathSeparator)+1:]
|
defaultInstallDir := workingDir[strings.LastIndexByte(workingDir, os.PathSeparator)+1:]
|
||||||
|
|
||||||
// init the cli app
|
// 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
|
// version command
|
||||||
app.Command(cli.Command("version", "\t prints your iris version").Action(func(cli.Flags) error { app.Printf("%s", iris.Version); return nil }))
|
app.Command(cli.Command("version", "\t prints your iris version").Action(func(cli.Flags) error { app.Printf("%s", iris.Version); return nil }))
|
||||||
|
|
||||||
|
|
26
iris/run.go
26
iris/run.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -38,6 +39,7 @@ func build(sourcepath string) error {
|
||||||
func run(executablePath string, stdout bool) (*utils.Cmd, error) {
|
func run(executablePath string, stdout bool) (*utils.Cmd, error) {
|
||||||
runCmd := utils.CommandBuilder("." + utils.PathSeparator + executablePath)
|
runCmd := utils.CommandBuilder("." + utils.PathSeparator + executablePath)
|
||||||
runCmd.Dir = workingDir
|
runCmd.Dir = workingDir
|
||||||
|
runCmd.Stderr = os.Stderr
|
||||||
if stdout {
|
if stdout {
|
||||||
runCmd.Stdout = os.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
|
// 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)
|
//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:]
|
fname = fname[len(workingDir)+1:]
|
||||||
|
|
||||||
|
@ -101,14 +118,17 @@ func runAndWatch(flags cli.Flags) error {
|
||||||
}, printer)
|
}, printer)
|
||||||
|
|
||||||
if err := build(programPath); err != nil {
|
if err := build(programPath); err != nil {
|
||||||
|
printer.Dangerf(err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
runCmd, err := run(executablePath, true)
|
runCmd, err := run(executablePath, true)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
printer.Dangerf(err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// here(below), we don't return the error because the -help command doesn't help the user for these errors.
|
||||||
defer func() {
|
defer func() {
|
||||||
printer.Dangerf("")
|
printer.Dangerf("")
|
||||||
printer.Panic(errUnexpected)
|
printer.Panic(errUnexpected)
|
||||||
|
|
|
@ -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.
|
// 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"
|
isWindows := runtime.GOOS == "windows"
|
||||||
watcher, werr := fsnotify.NewWatcher()
|
watcher, werr := fsnotify.NewWatcher()
|
||||||
if werr != nil {
|
if werr != nil {
|
||||||
|
@ -357,7 +357,7 @@ func WatchDirectoryChanges(rootPath string, evt func(filename string), logger *l
|
||||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||||
//this is received two times, the last time is the real changed file, so
|
//this is received two times, the last time is the real changed file, so
|
||||||
i++
|
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)) {
|
if time.Now().After(lastChange.Add(time.Duration(1) * time.Second)) {
|
||||||
lastChange = time.Now()
|
lastChange = time.Now()
|
||||||
evt(event.Name)
|
evt(event.Name)
|
||||||
|
@ -370,11 +370,10 @@ func WatchDirectoryChanges(rootPath string, evt func(filename string), logger *l
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
for _, p := range paths {
|
||||||
werr = watcher.Add(rootPath)
|
if werr = watcher.Add(p); werr != nil {
|
||||||
if werr != nil {
|
logger.Dangerf(werr.Error())
|
||||||
logger.Dangerf(werr.Error())
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user