Skip to content

Commit

Permalink
cmd: add outDir to Create
Browse files Browse the repository at this point in the history
This will allow us to generate projects into any directory we would like.

We also:

  - replace spome Sprintfs with path.Join
  - Use MkdirAll instead of MkDir
  - Return the project directory

Signed-off-by: William Casarin <jb55@jb55.com>
  • Loading branch information
jb55 committed Oct 11, 2019
1 parent 5f87969 commit aee97da
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions cmd/create.go
@@ -1,9 +1,9 @@
package cmd

import (
"fmt"
"log"
"os"
"path"

"github.com/spf13/cobra"
)
Expand All @@ -13,32 +13,34 @@ func init() {
rootCmd.AddCommand(createCmd)
}

func Create(projectName string) {
rootDir := fmt.Sprintf("./%v", projectName)

func Create(projectName string, outDir string) string {
rootDir := path.Join(outDir, projectName)
log.Printf("Creating project %s.", projectName)
err := os.MkdirAll(rootDir, os.ModePerm)

err := os.Mkdir(rootDir, os.ModePerm)
if os.IsExist(err) {
log.Fatalf("Directory %v already exists! Error: %v", projectName, err)
} else if err != nil {
log.Fatalf("Error creating root: %v ", err)
}

commit0ConfigPath := fmt.Sprintf("%v/commit0.yml", rootDir)
commit0ConfigPath := path.Join(rootDir, "commit0.yml")
log.Printf("%s", commit0ConfigPath)

f, err := os.Create(commit0ConfigPath)
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
Templator.Commit0.Execute(f, projectName)

gitIgnorePath := fmt.Sprintf("%v/.gitignore", rootDir)
gitIgnorePath := path.Join(rootDir, ".gitignore")
f, err = os.Create(gitIgnorePath)
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
Templator.GitIgnore.Execute(f, projectName)

return rootDir
}

var createCmd = &cobra.Command{
Expand All @@ -51,6 +53,6 @@ var createCmd = &cobra.Command{

projectName := args[0]

Create(projectName)
Create(projectName, "./")
},
}

0 comments on commit aee97da

Please sign in to comment.