mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
Update iris tool ' iris run main.go`
This commit is contained in:
parent
e9a4746000
commit
16e32fb25a
|
@ -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.5
|
Current version: 0.0.6
|
||||||
```sh
|
```sh
|
||||||
|
|
||||||
go get -u github.com/kataras/iris/iris
|
go get -u github.com/kataras/iris/iris
|
||||||
|
|
|
@ -33,7 +33,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.5")
|
app = cli.NewApp("iris", "Command line tool for Iris web framework", "0.0.6")
|
||||||
// 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 }))
|
||||||
|
|
||||||
|
|
94
iris/run.go
94
iris/run.go
|
@ -17,16 +17,49 @@ var (
|
||||||
errInvalidArgs = errors.New("Invalid arguments [%s], type -h to get assistant")
|
errInvalidArgs = errors.New("Invalid arguments [%s], type -h to get assistant")
|
||||||
errInvalidExt = errors.New("%s is not a go program")
|
errInvalidExt = errors.New("%s is not a go program")
|
||||||
errUnexpected = errors.New("Unexpected error!!! Please post an issue here: https://github.com/kataras/iris/issues")
|
errUnexpected = errors.New("Unexpected error!!! Please post an issue here: https://github.com/kataras/iris/issues")
|
||||||
|
errBuild = errors.New("\n Failed to build the %s iris program. Trace: %s")
|
||||||
|
errRun = errors.New("\n Failed to run the %s iris program. Trace: %s")
|
||||||
goExt = ".go"
|
goExt = ".go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func build(sourcepath string) error {
|
||||||
|
goBuild := utils.CommandBuilder("go", "build", sourcepath)
|
||||||
|
goBuild.Dir = workingDir
|
||||||
|
goBuild.Stdout = os.Stdout
|
||||||
|
goBuild.Stderr = os.Stderr
|
||||||
|
if err := goBuild.Run(); err != nil {
|
||||||
|
ferr := errBuild.Format(sourcepath, err.Error())
|
||||||
|
printer.Dangerf(ferr.Error())
|
||||||
|
return ferr
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(executablePath string, stdout bool) (*utils.Cmd, error) {
|
||||||
|
runCmd := utils.CommandBuilder("." + utils.PathSeparator + executablePath)
|
||||||
|
runCmd.Dir = workingDir
|
||||||
|
if stdout {
|
||||||
|
runCmd.Stdout = os.Stdout
|
||||||
|
}
|
||||||
|
|
||||||
|
runCmd.Stderr = os.Stderr
|
||||||
|
if err := runCmd.Start(); err != nil {
|
||||||
|
ferr := errRun.Format(executablePath, err.Error())
|
||||||
|
printer.Dangerf(ferr.Error())
|
||||||
|
return nil, ferr
|
||||||
|
}
|
||||||
|
return runCmd, nil
|
||||||
|
}
|
||||||
|
|
||||||
func runAndWatch(flags cli.Flags) error {
|
func runAndWatch(flags cli.Flags) error {
|
||||||
if len(os.Args) <= 2 {
|
if len(os.Args) <= 2 {
|
||||||
err := errInvalidArgs.Format(strings.Join(os.Args, ","))
|
err := errInvalidArgs.Format(strings.Join(os.Args, ","))
|
||||||
printer.Dangerf(err.Error())
|
printer.Dangerf(err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
isWindows := runtime.GOOS == "windows"
|
||||||
programPath := ""
|
programPath := ""
|
||||||
|
executablePath := ""
|
||||||
filenameCh := make(chan string)
|
filenameCh := make(chan string)
|
||||||
|
|
||||||
if len(os.Args) > 2 { // iris run main.go
|
if len(os.Args) > 2 { // iris run main.go
|
||||||
|
@ -38,6 +71,11 @@ func runAndWatch(flags cli.Flags) error {
|
||||||
if filepath.Ext(programPath) != goExt {
|
if filepath.Ext(programPath) != goExt {
|
||||||
return errInvalidExt.Format(programPath)
|
return errInvalidExt.Format(programPath)
|
||||||
}
|
}
|
||||||
|
executablePath = programPath[:len(programPath)-3]
|
||||||
|
if isWindows {
|
||||||
|
executablePath += ".exe"
|
||||||
|
}
|
||||||
|
println("executablePath: " + executablePath)
|
||||||
}
|
}
|
||||||
// here(below), we don't return the error because the -help command doesn't help the user for these errors.
|
// here(below), we don't return the error because the -help command doesn't help the user for these errors.
|
||||||
|
|
||||||
|
@ -49,17 +87,15 @@ func runAndWatch(flags cli.Flags) error {
|
||||||
|
|
||||||
}, printer)
|
}, printer)
|
||||||
|
|
||||||
// we don't use go build and run from the executable, for performance reasons, no need this is a development action already
|
if err := build(programPath); err != nil {
|
||||||
goRun := utils.CommandBuilder("go", "run", programPath)
|
return err
|
||||||
goRun.Dir = workingDir
|
}
|
||||||
goRun.Stdout = os.Stdout
|
|
||||||
goRun.Stderr = os.Stderr
|
runCmd, err := run(executablePath, true)
|
||||||
if err := goRun.Start(); err != nil {
|
if err != nil {
|
||||||
printer.Dangerf("\n [ERROR] Failed to run the %s iris program. Trace: %s", programPath, err.Error())
|
return err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isWindows := runtime.GOOS == "windows"
|
|
||||||
defer func() {
|
defer func() {
|
||||||
printer.Dangerf("")
|
printer.Dangerf("")
|
||||||
printer.Panic(errUnexpected)
|
printer.Panic(errUnexpected)
|
||||||
|
@ -70,25 +106,37 @@ func runAndWatch(flags cli.Flags) error {
|
||||||
case fname := <-filenameCh:
|
case fname := <-filenameCh:
|
||||||
{
|
{
|
||||||
// it's not a warning but I like to use purple color for this message
|
// it's not a warning but I like to use purple color for this message
|
||||||
printer.Warningf("\n/-%d-/ File '%s' changed, re-running...", atomic.LoadUint32(×), fname)
|
printer.Infof("\n%d- File '%s' changed, reloading...", atomic.LoadUint32(×), fname)
|
||||||
// force kill, sometimes runCmd.Process.Kill or Signal(os.Kill) doesn't kill the child of the go's go run command ( which is the iris program)
|
|
||||||
if isWindows {
|
//kill the prev run
|
||||||
utils.CommandBuilder("taskkill", "/F", "/T", "/PID", strconv.Itoa(goRun.Process.Pid)).Run()
|
|
||||||
|
err := runCmd.Process.Kill()
|
||||||
|
if err == nil {
|
||||||
|
_, err = runCmd.Process.Wait()
|
||||||
} else {
|
} else {
|
||||||
utils.CommandBuilder("kill", "-INT", "-"+strconv.Itoa(goRun.Process.Pid)).Run()
|
|
||||||
|
// force kill, sometimes runCmd.Process.Kill or Signal(os.Kill) doesn't kills
|
||||||
|
if isWindows {
|
||||||
|
err = utils.CommandBuilder("taskkill", "/F", "/T", "/PID", strconv.Itoa(runCmd.Process.Pid)).Run()
|
||||||
|
} else {
|
||||||
|
err = utils.CommandBuilder("kill", "-INT", "-"+strconv.Itoa(runCmd.Process.Pid)).Run()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
goRun = utils.CommandBuilder("go", "run", programPath)
|
err = build(programPath)
|
||||||
goRun.Dir = workingDir
|
if err != nil {
|
||||||
goRun.Stderr = os.Stderr
|
printer.Warningf(err.Error())
|
||||||
|
|
||||||
if err := goRun.Start(); err != nil {
|
|
||||||
printer.Warningf("\n [ERROR ON RELOAD] Failed to run the %s iris program. Trace: %s", programPath, err.Error())
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
atomic.AddUint32(×, 1)
|
|
||||||
// don't print success on anything here because we may have error on iris itself, no need to print any message we are no spammers.
|
if runCmd, err = run(executablePath, false); err != nil {
|
||||||
|
printer.Warningf(err.Error())
|
||||||
|
|
||||||
|
} else {
|
||||||
|
printer.Successf("ready!")
|
||||||
|
atomic.AddUint32(×, 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user