This commit is contained in:
Makis Maropoulos 2016-06-28 10:19:17 +03:00
parent 0a896f5137
commit 04dbd0bac9
4 changed files with 44 additions and 31 deletions

View File

@ -275,7 +275,7 @@ func (ctx *Context) RequestHeader(k string) string {
// PostFormValue returns a single value from post request's data // PostFormValue returns a single value from post request's data
func (ctx *Context) PostFormValue(name string) string { 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 // PostFormMulti returns a slice of string from post request's data

View File

@ -3,6 +3,7 @@ package main
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -21,23 +22,41 @@ var (
packagesInstallDir = utils.AssetsDirectory + utils.PathSeparator + "iris-command-assets" + utils.PathSeparator 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) { 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") { if !utils.DirectoryExists(packagesInstallDir) || !flags.Bool("offline") {
downloadPackages() 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) createPackage(flags.String("type"), targetDir)
return return
} }
@ -66,7 +85,7 @@ func downloadPackages() {
} }
func createPackage(packageName string, targetDir string) error { 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 packageDir := packagesInstallDir + utils.PathSeparator + packageName
err := utils.CopyDir(packageDir, installTo) 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) 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 { if err != nil {
printer.Warningf("Error while preparing main file: %#v", err) printer.Warningf("Error while preparing main file: %#v", err)
} }

View File

@ -3,10 +3,6 @@ package main
import ( import (
"os" "os"
_ "syscall"
"strings"
"github.com/kataras/cli" "github.com/kataras/cli"
"github.com/kataras/iris" "github.com/kataras/iris"
"github.com/kataras/iris/config" "github.com/kataras/iris/config"
@ -33,10 +29,6 @@ func init() {
workingDir = d 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 // init the cli app
app = cli.NewApp("iris", "Command line tool for Iris web framework", Version) app = cli.NewApp("iris", "Command line tool for Iris web framework", Version)
// version command // version command
@ -45,7 +37,7 @@ func init() {
// create command/-/create.go // create command/-/create.go
createCmd := cli.Command("create", "create a project to a given directory"). 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("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'"). Flag("type", "basic", "creates a project based on the -t package. Currently, available types are 'basic' & 'static'").
Action(create) Action(create)

View File

@ -16,14 +16,16 @@ func runAndWatch(flags cli.Flags) error {
} }
programPath := os.Args[2] programPath := os.Args[2]
/*project := rizla.NewProject(programPath) /*
project := rizla.NewProject(programPath)
project.Name = "IRIS" project.Name = "IRIS"
project.AllowReloadAfter = time.Duration(3) * time.Second project.AllowReloadAfter = time.Duration(3) * time.Second
project.Out = rizla.NewPrinter(os.Stdout)
project.Err = rizla.NewPrinter(os.Stderr)
rizla.Add(project) rizla.Add(project)
rizla.Out = os.Stdout rizla.Run()
rizla.Err = os.Stderr */
rizla.Run()*/
// or just do that: // or just do that:
rizla.Run(programPath) rizla.Run(programPath)