diff --git a/cmd/alfreddd/boilerplate.go b/cmd/alfreddd/boilerplate.go index d31d0d4..a4d7ce1 100644 --- a/cmd/alfreddd/boilerplate.go +++ b/cmd/alfreddd/boilerplate.go @@ -23,22 +23,6 @@ func makeBoilerplate(ctx *cli.Context) (err error) { appName := ctx.String(appFlag) noApiSpec := ctx.Bool(apiFlag) - if len(targetFolder) <= 0 { - defaultFolder, _ := filepath.Abs(".") - fmt.Printf("What's the folder where I'm going to create the local repository? [Press ENTER to use %s]\n", defaultFolder) - fmt.Scanln(&targetFolder) - - if len(targetFolder) <= 0 { - targetFolder = defaultFolder - } - } - targetFolder, _ = filepath.Abs(targetFolder) - - targetFolderCreated, err := makeDirectoryIfNotExists(targetFolder) - if err != nil { - return fmt.Errorf("failed to create folder %s: %s", targetFolder, err) - } - if len(projectName) <= 0 { for { fmt.Println("\nWhat's the name of the GH project? [Please enter a name in the form /]") @@ -53,7 +37,24 @@ func makeBoilerplate(ctx *cli.Context) (err error) { } } - projectPath := filepath.Join(targetFolder, projectName) + if len(targetFolder) <= 0 { + defaultFolder, _ := filepath.Abs(".") + defaultFolder = filepath.Join(defaultFolder, projectName) + fmt.Printf("\nWhat's the folder where I'm going to create the local repository? [Press ENTER to use %s]\n", defaultFolder) + fmt.Scanln(&targetFolder) + targetFolder = cleanAndExpandPath(strings.Trim(targetFolder, " \n")) + if err != nil { + return err + } + if len(targetFolder) <= 0 { + targetFolder = defaultFolder + } + } + targetFolder, _ = filepath.Abs(targetFolder) + + if err := makeDirectoryIfNotExists(targetFolder); err != nil { + return fmt.Errorf("failed to create folder %s: %s", targetFolder, err) + } defer func() { if err != nil { @@ -75,11 +76,7 @@ func makeBoilerplate(ctx *cli.Context) (err error) { } if delete { - dir := projectPath - if targetFolderCreated { - dir = targetFolder - } - os.RemoveAll(dir) + os.RemoveAll(targetFolder) } } }() @@ -122,7 +119,7 @@ func makeBoilerplate(ctx *cli.Context) (err error) { } fmt.Println("\nI'm creating the local repository...") - if err := makeScaffolding(projectPath, projectName, moduleName, appName, noApiSpec); err != nil { + if err := makeScaffolding(targetFolder, projectName, moduleName, appName, noApiSpec); err != nil { return fmt.Errorf("i couldn't create the local repository for the following reason: %s", err) } fmt.Println("Done!") @@ -131,13 +128,13 @@ func makeBoilerplate(ctx *cli.Context) (err error) { fmt.Scanln() fmt.Println("\nAlright! I'm initializing the project...") - if err := initProject(projectPath, projectName, moduleName, noApiSpec); err != nil { + if err := initProject(targetFolder, projectName, moduleName, noApiSpec); err != nil { return fmt.Errorf("i couldn't initialize the project for the following reason: %s", err) } fmt.Println("\nDone!") green.Println("\nYour project has been kick-started") - green.Printf("\nLocal repo: %s\n", projectPath) + green.Printf("\nLocal repo: %s\n", targetFolder) green.Printf("Remote repo: https://github.com/%s\n", projectName) return nil diff --git a/cmd/alfreddd/scaffolding.go b/cmd/alfreddd/scaffolding.go index f54a45f..3613671 100644 --- a/cmd/alfreddd/scaffolding.go +++ b/cmd/alfreddd/scaffolding.go @@ -34,7 +34,7 @@ func makeScaffolding(folder, ghProject, module, app string, noApiSpec bool) erro for dir, isEmpty := range dirs { path := filepath.Join(folder, dir) - if _, err := makeDirectoryIfNotExists(path); err != nil { + if err := makeDirectoryIfNotExists(path); err != nil { return err } if isEmpty { diff --git a/cmd/alfreddd/utils.go b/cmd/alfreddd/utils.go index d1faad2..44ad0f2 100644 --- a/cmd/alfreddd/utils.go +++ b/cmd/alfreddd/utils.go @@ -34,14 +34,11 @@ func cleanAndExpandPath(path string) string { return filepath.Clean(os.ExpandEnv(path)) } -func makeDirectoryIfNotExists(path string) (bool, error) { +func makeDirectoryIfNotExists(path string) error { if _, err := os.Stat(path); os.IsNotExist(err) { - if err := os.MkdirAll(path, os.ModeDir|0755); err != nil { - return false, err - } - return true, nil + return os.MkdirAll(path, os.ModeDir|0755) } - return false, nil + return nil } func formatVersion() string {