From 0e05c68876306aaec9b1f27092a9a3802f5c46fb Mon Sep 17 00:00:00 2001 From: Makis Maropoulos Date: Tue, 7 Jun 2016 12:21:40 +0300 Subject: [PATCH] Update Iris tool to v.0.0.4 - Install dir now is based on $GOPATH/src --- iris/README.md | 17 +++++++++-------- iris/main.go | 47 ++++++++++++++++++++++++++++++++++------------- utils/file.go | 6 +++++- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/iris/README.md b/iris/README.md index a78c58ff..b2a1fad4 100644 --- a/iris/README.md +++ b/iris/README.md @@ -2,9 +2,10 @@ This package is the command line tool for [../](https://github.com/kataras/iris). +[!Iris help screen](https://raw.githubusercontent.com/iris-contrib/website/gh-pages/assets/iris_cli_screen.png)](https://raw.githubusercontent.com/iris-contrib/website/gh-pages/assets/iris_cli_screen.png) ## Install -Current version: 0.0.3 +Current version: 0.0.4 ```sh go get -u github.com/kataras/iris/iris @@ -28,16 +29,16 @@ $ iris [command] [-flags] ```sh -iris create -t basic -d ./ +iris create -t basic -d myprojects/iris1 ``` -Will create the [basic](https://github.com/iris-contrib/iris-command-assets/tree/master/basic) sample package to the current working directory and run the app. +Will create the [basic](https://github.com/iris-contrib/iris-command-assets/tree/master/basic) sample package to the `$GOPATH/src/myprojects/iris1` directory and run the app. ```sh -iris create -t static -d ./ +iris create -t static -d myprojects/iris1 ``` -Will create the [static](https://github.com/iris-contrib/iris-command-assets/tree/master/static) sample package to the current working directory and run the app. +Will create the [static](https://github.com/iris-contrib/iris-command-assets/tree/master/static) sample package to the `$GOPATH/src/myprojects/iris1` directory and run the app. The default @@ -46,13 +47,13 @@ The default iris create ``` -Will create the basic sample package to the current working directory and run the app. +Will create the basic sample package to `$GOPATH/src/myiris` directory and run the app. ```sh -iris create -d C:\Users\kataras\Desktop\test1 +iris create -d myproject ``` -Will create the basic sample package to the C:\Users\kataras\Desktop\test1 folder and run the app. +Will create the basic sample package to the `$GOPATH/src/myproject` folder and run the app. ## Version diff --git a/iris/main.go b/iris/main.go index 67a2f61e..000210ca 100644 --- a/iris/main.go +++ b/iris/main.go @@ -5,6 +5,7 @@ import ( "strings" + "io/ioutil" "runtime" "github.com/fatih/color" @@ -30,13 +31,13 @@ var ( ) func init() { - app = cli.NewApp("iris", "Command line tool for Iris web framework", "0.0.3") + app = cli.NewApp("iris", "Command line tool for Iris web framework", "0.0.4") app.Command(cli.Command("version", "\t prints your iris version").Action(func(cli.Flags) error { app.Printf("%s", iris.Version); return nil })) createCmd := cli.Command("create", "create a project to a given directory"). Flag("offline", false, "set to true to disable the packages download on each create command"). - Flag("dir", "./", "creates an iris starter kit to the current directory"). - Flag("type", "basic", "creates the project based on the -t package. Currently, available types are 'basic' & 'static'"). + Flag("dir", "myiris", "$GOPATH/src/$dir the directory to install the sample package"). + Flag("type", "basic", "creates a project based on the -t package. Currently, available types are 'basic' & 'static'"). Action(create) app.Command(createCmd) @@ -54,13 +55,14 @@ func create(flags cli.Flags) (err error) { targetDir := flags.String("dir") + // remove first and last / if any if strings.HasPrefix(targetDir, "./") || strings.HasPrefix(targetDir, "."+utils.PathSeparator) { - currentWdir, err := os.Getwd() - if err != nil { - return err - } - targetDir = currentWdir + utils.PathSeparator + targetDir[2:] + targetDir = targetDir[2:] } + if targetDir[len(targetDir)-1] == '/' { + targetDir = targetDir[0 : len(targetDir)-1] + } + // createPackage(flags.String("type"), targetDir) return @@ -90,23 +92,42 @@ func downloadPackages() { } func createPackage(packageName string, targetDir string) error { + installTo := os.Getenv("GOPATH") + utils.PathSeparator + "src" + utils.PathSeparator + targetDir + packageDir := packagesInstallDir + utils.PathSeparator + packageName - err := utils.CopyDir(packageDir, targetDir) + err := utils.CopyDir(packageDir, installTo) if err != nil { - app.Printf("\nProblem while copying the %s package to the %s. Trace: %s", packageName, targetDir, err.Error()) + app.Printf("\nProblem while copying the %s package to the %s. Trace: %s", packageName, installTo, err.Error()) return err } + // now replace main.go's 'github.com/iris-contrib/iris-command-assets/basic/' with targetDir + // hardcode all that, we don't have anything special and neither will do + targetDir = strings.Replace(targetDir, "\\", "/", -1) // for any case + mainFile := installTo + utils.PathSeparator + "backend" + utils.PathSeparator + "main.go" + + input, err := ioutil.ReadFile(mainFile) + if err != nil { + app.Printf("Error while preparing main file: %#v", err) + } + + output := strings.Replace(string(input), "github.com/iris-contrib/iris-command-assets/"+packageName+"/", targetDir+"/", -1) + + err = ioutil.WriteFile(mainFile, []byte(output), 0644) + if err != nil { + app.Printf("Error while preparing main file: %#v", err) + } + InfoPrint("\n%s package was installed successfully", packageName) // build & run the server // go build buildCmd := utils.CommandBuilder("go", "build") - if targetDir[len(targetDir)-1] != os.PathSeparator || targetDir[len(targetDir)-1] != '/' { - targetDir += utils.PathSeparator + if installTo[len(installTo)-1] != os.PathSeparator || installTo[len(installTo)-1] != '/' { + installTo += utils.PathSeparator } - buildCmd.Dir = targetDir + "backend" + buildCmd.Dir = installTo + "backend" buildCmd.Stderr = os.Stderr err = buildCmd.Start() if err != nil { diff --git a/utils/file.go b/utils/file.go index 1cc8faa1..2cdd8165 100644 --- a/utils/file.go +++ b/utils/file.go @@ -33,6 +33,9 @@ func DownloadZip(zipURL string, newDir string) (string, error) { var err error var size int64 finish := make(chan bool) + defer func() { + finish <- true + }() go func() { print("\n|") @@ -90,7 +93,8 @@ func DownloadZip(zipURL string, newDir string) (string, error) { return "", ErrFileCopy.Format(err.Error()) } finish <- true - print("OK ", size, " bytes downloaded") //we keep that here so developer will always see in the terminal if a plugin downloads something + _ = size + //print("OK ", size, " bytes downloaded") //we keep that here so developer will always see in the terminal if a plugin downloads something or no ? return fileName, nil }