From 04dbd0bac944d1c989d1447bbe320d2fa77ca6c8 Mon Sep 17 00:00:00 2001 From: Makis Maropoulos Date: Tue, 28 Jun 2016 10:19:17 +0300 Subject: [PATCH] Fix https://github.com/kataras/iris/issues/237 --- context.go | 2 +- iris/create.go | 47 +++++++++++++++++++++++++++++++++-------------- iris/main.go | 10 +--------- iris/run.go | 16 +++++++++------- 4 files changed, 44 insertions(+), 31 deletions(-) diff --git a/context.go b/context.go index 3acbeffb..80f521c7 100644 --- a/context.go +++ b/context.go @@ -275,7 +275,7 @@ func (ctx *Context) RequestHeader(k string) string { // PostFormValue returns a single value from post request's data func (ctx *Context) PostFormValue(name string) string { - return string(ctx.RequestCtx.PostArgs().Peek(name)) + return string(ctx.FormValue(name)) } // PostFormMulti returns a slice of string from post request's data diff --git a/iris/create.go b/iris/create.go index 230832b7..95b932c0 100644 --- a/iris/create.go +++ b/iris/create.go @@ -3,6 +3,7 @@ package main import ( "io/ioutil" "os" + "path/filepath" "runtime" "strings" @@ -21,23 +22,41 @@ var ( packagesInstallDir = utils.AssetsDirectory + utils.PathSeparator + "iris-command-assets" + utils.PathSeparator ) +func isValidInstallDir(targetDir string) bool { + // https://github.com/kataras/iris/issues/237 + gopath := os.Getenv("GOPATH") + // remove the last ;/: for any case before the split + if idxLSep := strings.IndexByte(gopath, os.PathListSeparator); idxLSep == len(gopath)-1 { + gopath = gopath[0 : len(gopath)-2] + } + + // check if we have more than one gopath + gopaths := strings.Split(gopath, string(os.PathListSeparator)) + // the package MUST be installed only inside a valid gopath, if not then print an error to the user. + for _, gpath := range gopaths { + if strings.HasPrefix(targetDir, gpath+utils.PathSeparator) { + return true + } + } + return false +} + func create(flags cli.Flags) (err error) { + targetDir, err := filepath.Abs(flags.String("dir")) + if err != nil { + panic(err) + } + + if !isValidInstallDir(targetDir) { + printer.Dangerf("\nPlease make sure you are targeting a directory inside $GOPATH, type iris -h for help.") + return + } + if !utils.DirectoryExists(packagesInstallDir) || !flags.Bool("offline") { downloadPackages() } - targetDir := flags.String("dir") - - // remove first and last / if any - if strings.HasPrefix(targetDir, "./") || strings.HasPrefix(targetDir, "."+utils.PathSeparator) { - targetDir = targetDir[2:] - } - if targetDir[len(targetDir)-1] == '/' { - targetDir = targetDir[0 : len(targetDir)-1] - } - // - createPackage(flags.String("type"), targetDir) return } @@ -66,7 +85,7 @@ func downloadPackages() { } func createPackage(packageName string, targetDir string) error { - installTo := os.Getenv("GOPATH") + utils.PathSeparator + "src" + utils.PathSeparator + targetDir + installTo := targetDir // os.Getenv("GOPATH") + utils.PathSeparator + "src" + utils.PathSeparator + targetDir packageDir := packagesInstallDir + utils.PathSeparator + packageName err := utils.CopyDir(packageDir, installTo) @@ -85,9 +104,9 @@ func createPackage(packageName string, targetDir string) error { printer.Warningf("Error while preparing main file: %#v", err) } - output := strings.Replace(string(input), "github.com/iris-contrib/iris-command-assets/"+packageName+"/", targetDir+"/", -1) + output := strings.Replace(string(input), "github.com/iris-contrib/iris-command-assets/"+packageName+"/", filepath.Base(targetDir)+"/", -1) - err = ioutil.WriteFile(mainFile, []byte(output), 0644) + err = ioutil.WriteFile(mainFile, []byte(output), 0777) if err != nil { printer.Warningf("Error while preparing main file: %#v", err) } diff --git a/iris/main.go b/iris/main.go index d4b2272a..d3eccb91 100644 --- a/iris/main.go +++ b/iris/main.go @@ -3,10 +3,6 @@ package main import ( "os" - _ "syscall" - - "strings" - "github.com/kataras/cli" "github.com/kataras/iris" "github.com/kataras/iris/config" @@ -33,10 +29,6 @@ func init() { workingDir = d } - // defaultInstallDir is the default directory which the create will copy and run the package when finish downloading - // it's just the last path part of the workingDir - defaultInstallDir := workingDir[strings.LastIndexByte(workingDir, os.PathSeparator)+1:] - // init the cli app app = cli.NewApp("iris", "Command line tool for Iris web framework", Version) // version command @@ -45,7 +37,7 @@ func init() { // create command/-/create.go 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", defaultInstallDir, "$GOPATH/src/$dir the directory to install the sample package"). + Flag("dir", workingDir, "$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) diff --git a/iris/run.go b/iris/run.go index 4c32bdad..2502aa7d 100644 --- a/iris/run.go +++ b/iris/run.go @@ -16,14 +16,16 @@ func runAndWatch(flags cli.Flags) error { } programPath := os.Args[2] - /*project := rizla.NewProject(programPath) - project.Name = "IRIS" - project.AllowReloadAfter = time.Duration(3) * time.Second - rizla.Add(project) + /* + project := rizla.NewProject(programPath) + project.Name = "IRIS" + project.AllowReloadAfter = time.Duration(3) * time.Second + project.Out = rizla.NewPrinter(os.Stdout) + project.Err = rizla.NewPrinter(os.Stderr) + rizla.Add(project) - rizla.Out = os.Stdout - rizla.Err = os.Stderr - rizla.Run()*/ + rizla.Run() + */ // or just do that: rizla.Run(programPath)