diff --git a/model/command_config.go b/model/command_config.go index c548cad..4b6d9e5 100644 --- a/model/command_config.go +++ b/model/command_config.go @@ -92,7 +92,7 @@ type ( ) // Updates the import path depending on the command -func (c *CommandConfig) UpdateImportPath() bool { +func (c *CommandConfig) UpdateImportPath() error { var importPath string required := true switch c.Index { @@ -114,6 +114,7 @@ func (c *CommandConfig) UpdateImportPath() bool { } if len(importPath) == 0 || filepath.IsAbs(importPath) || importPath[0] == '.' { + utils.Logger.Info("Import path is absolute or not specified", "path", importPath) // Try to determine the import path from the GO paths and the command line currentPath, err := os.Getwd() if len(importPath) > 0 { @@ -135,7 +136,7 @@ func (c *CommandConfig) UpdateImportPath() bool { if len(importPath) > 4 && strings.ToLower(importPath[0:4]) == "src/" { importPath = importPath[4:] } else if importPath == "src" { - importPath = "" + return fmt.Errorf("Invlaid import path, working dir is in GOPATH root") } utils.Logger.Info("Updated import path", "path", importPath) } @@ -155,7 +156,13 @@ func (c *CommandConfig) UpdateImportPath() bool { } utils.Logger.Info("Revel versions", "revel-tool", c.CommandVersion.String(), "Revel Framework", c.FrameworkVersion.String()) } - return (len(importPath) > 0 || !required) + if !required { + return nil + } + if len(importPath) == 0 { + return fmt.Errorf("Unable to determine import path from : %s", importPath) + } + return nil } // Used to initialize the package resolver diff --git a/model/revel_container.go b/model/revel_container.go index 64ad4f3..1cba47c 100644 --- a/model/revel_container.go +++ b/model/revel_container.go @@ -120,8 +120,16 @@ func NewRevelPaths(mode, importPath, srcPath string, callback RevelCallback) (rp rp.BasePath = filepath.Join(rp.SourcePath, filepath.FromSlash(importPath)) rp.PackageInfo.Vendor = utils.Exists(filepath.Join(rp.BasePath, "vendor")) rp.AppPath = filepath.Join(rp.BasePath, "app") - rp.ViewsPath = filepath.Join(rp.AppPath, "views") + // Sanity check , ensure app and conf paths exist + if !utils.DirExists(rp.AppPath) { + return rp, fmt.Errorf("No application found at path %s", rp.AppPath) + } + if !utils.DirExists(filepath.Join(rp.BasePath, "conf")) { + return rp, fmt.Errorf("No configuration found at path %s", filepath.Join(rp.BasePath, "conf")) + } + + rp.ViewsPath = filepath.Join(rp.AppPath, "views") rp.CodePaths = []string{rp.AppPath} rp.TemplatePaths = []string{} diff --git a/revel/command_test.go b/revel/command_test.go index cf1dcf8..01e52f8 100644 --- a/revel/command_test.go +++ b/revel/command_test.go @@ -65,7 +65,7 @@ func newApp(name string, command model.COMMAND, precall func(c *model.CommandCon if precall != nil { precall(c) } - if !c.UpdateImportPath() { + if c.UpdateImportPath()!=nil { a.Fail("Unable to update import path") } c.InitGoPaths() diff --git a/revel/new.go b/revel/new.go index f74e8de..ed22bda 100644 --- a/revel/new.go +++ b/revel/new.go @@ -65,6 +65,13 @@ func newApp(c *model.CommandConfig) (err error) { if err == nil || !utils.Empty(c.AppPath) { return utils.NewBuildError("Abort: Import path already exists.", "path", c.ImportPath) } + + // checking and setting skeleton + if err=setSkeletonPath(c);err!=nil { + return + } + + // Create application path if err := os.MkdirAll(c.AppPath, os.ModePerm); err != nil { return utils.NewBuildError("Abort: Unable to create app path.", "path", c.AppPath) } @@ -115,18 +122,12 @@ func newApp(c *model.CommandConfig) (err error) { } // checking and setting application - if err = setApplicationPath(c); err != nil { return err } // At this point the versions can be set c.SetVersions() - // checking and setting skeleton - if err=setSkeletonPath(c);err!=nil { - return - } - // copy files to new app directory if err = copyNewAppFiles(c);err != nil { return @@ -201,11 +202,28 @@ func setSkeletonPath(c *model.CommandConfig) (err error) { } // First check to see the protocol of the string - if sp, err := url.Parse(c.New.SkeletonPath); err == nil { + sp, err := url.Parse(c.New.SkeletonPath) + if err == nil { utils.Logger.Info("Detected skeleton path", "path", sp) switch strings.ToLower(sp.Scheme) { - // TODO Add support for https, http, ftp, file + // TODO Add support for https, http, ftp + case "" : + sp.Scheme="file" + fallthrough + case "file" : + fullpath := sp.String()[7:] + if !filepath.IsAbs(fullpath) { + fullpath, err = filepath.Abs(fullpath) + if err!=nil { + return + } + } + c.New.SkeletonPath = fullpath + utils.Logger.Info("Set skeleton path to ", fullpath) + if !utils.DirExists(fullpath) { + return fmt.Errorf("Failed to find skeleton in filepath %s %s", fullpath, sp.String()) + } case "git": if err := newLoadFromGit(c, sp); err != nil { return err @@ -237,7 +255,7 @@ func newLoadFromGit(c *model.CommandConfig, sp *url.URL) (err error) { utils.Logger.Info("Exec:", "args", getCmd.Args) getOutput, err := getCmd.CombinedOutput() if err != nil { - utils.Logger.Fatal("Abort: could not clone the Skeleton source code: ","output", getOutput, "path", c.New.SkeletonPath) + utils.Logger.Fatal("Abort: could not clone the Skeleton source code: ","output", string(getOutput), "path", c.New.SkeletonPath) } outputPath := targetPath if len(pathpart) > 1 { diff --git a/revel/revel.go b/revel/revel.go index ab15574..dd95b43 100644 --- a/revel/revel.go +++ b/revel/revel.go @@ -20,6 +20,7 @@ import ( "github.com/revel/cmd/logger" "github.com/revel/cmd/model" "github.com/revel/cmd/utils" + "bytes" ) const ( @@ -70,14 +71,17 @@ func main() { wd, _ := os.Getwd() utils.InitLogger(wd, logger.LvlError) - parser := flags.NewParser(c, flags.HelpFlag|flags.PassDoubleDash) - if err := ParseArgs(c, parser, os.Args[1:]); err != nil { - fmt.Fprint(os.Stderr, err.Error()) + if len(os.Args)<2 { parser.WriteHelp(os.Stdout) os.Exit(1) } + if err := ParseArgs(c, parser, os.Args[1:]); err != nil { + fmt.Fprint(os.Stderr, err.Error() +"\n") + os.Exit(1) + } + // Switch based on the verbose flag if len(c.Verbose)>1 { utils.InitLogger(wd, logger.LvlDebug) @@ -87,8 +91,10 @@ func main() { utils.InitLogger(wd, logger.LvlWarn) } - if !c.UpdateImportPath() { - utils.Logger.Fatal("Unable to determine application path") + if err := c.UpdateImportPath();err!=nil { + utils.Logger.Error(err.Error()) + parser.WriteHelp(os.Stdout) + os.Exit(1) } command := Commands[c.Index] @@ -136,12 +142,12 @@ func ParseArgs(c *model.CommandConfig, parser *flags.Parser, args []string) (err } } - if c.Index == 0 { - err = fmt.Errorf("Unknown command %v", extraArgs) - } else if len(extraArgs) > 0 { + if len(extraArgs) > 0 { utils.Logger.Info("Found additional arguements, setting them") if !Commands[c.Index].UpdateConfig(c, extraArgs) { - err = fmt.Errorf("Invalid command line arguements %v", extraArgs) + buffer := &bytes.Buffer{} + parser.WriteHelp(buffer) + err = fmt.Errorf("Invalid command line arguements %v\n%s", extraArgs, buffer.String()) } } diff --git a/revel/run.go b/revel/run.go index 40b4be0..c7bd5ee 100644 --- a/revel/run.go +++ b/revel/run.go @@ -13,6 +13,7 @@ import ( "github.com/revel/cmd/utils" "go/build" "os" + "path/filepath" ) var cmdRun = &Command{ @@ -72,7 +73,7 @@ func updateRunConfig(c *model.CommandConfig, args []string) bool { // 3. revel run [run-mode] [port] // Check to see if the import path evaluates out to something that may be on a gopath - if _, err := build.Import(args[0], "", build.FindOnly); err == nil { + if runIsImportPath(args[0]) { // 1st arg is the import path c.Run.ImportPath = args[0] @@ -93,12 +94,7 @@ func updateRunConfig(c *model.CommandConfig, args []string) bool { // 1. revel run [import-path] // 2. revel run [port] // 3. revel run [run-mode] - _, err := build.Import(args[0], "", build.FindOnly) - if err != nil { - utils.Logger.Warn("Unable to run using an import path, assuming import path is working directory %s %s", "Argument", args[0], "error", err.Error()) - } - utils.Logger.Info("Trying to build with", args[0], err) - if err == nil { + if runIsImportPath(args[0]) { // 1st arg is the import path c.Run.ImportPath = args[0] } else if _, err := strconv.Atoi(args[0]); err == nil { @@ -116,6 +112,14 @@ func updateRunConfig(c *model.CommandConfig, args []string) bool { return true } +// Returns true if this is an absolute path or a relative gopath +func runIsImportPath(pathToCheck string) bool { + if _, err := build.Import(pathToCheck, "", build.FindOnly);err==nil { + return true + } + return filepath.IsAbs(pathToCheck) +} + // Called to run the app func runApp(c *model.CommandConfig) (err error) { if c.Run.Mode == "" {