11package cmd
22
33import (
4- "fmt"
54 "log"
65 "os"
6+ "path"
77
88 "github.com/spf13/cobra"
99)
@@ -13,6 +13,36 @@ func init() {
1313 rootCmd .AddCommand (createCmd )
1414}
1515
16+ func Create (projectName string , outDir string ) string {
17+ rootDir := path .Join (outDir , projectName )
18+ log .Printf ("Creating project %s." , projectName )
19+ err := os .MkdirAll (rootDir , os .ModePerm )
20+
21+ if os .IsExist (err ) {
22+ log .Fatalf ("Directory %v already exists! Error: %v" , projectName , err )
23+ } else if err != nil {
24+ log .Fatalf ("Error creating root: %v " , err )
25+ }
26+
27+ commit0ConfigPath := path .Join (rootDir , "commit0.yml" )
28+ log .Printf ("%s" , commit0ConfigPath )
29+
30+ f , err := os .Create (commit0ConfigPath )
31+ if err != nil {
32+ log .Printf ("Error creating commit0 config: %v" , err )
33+ }
34+ Templator .Commit0 .Execute (f , projectName )
35+
36+ gitIgnorePath := path .Join (rootDir , ".gitignore" )
37+ f , err = os .Create (gitIgnorePath )
38+ if err != nil {
39+ log .Printf ("Error creating commit0 config: %v" , err )
40+ }
41+ Templator .GitIgnore .Execute (f , projectName )
42+
43+ return rootDir
44+ }
45+
1646var createCmd = & cobra.Command {
1747 Use : "create" ,
1848 Short : "Create new project with provided name." ,
@@ -23,30 +53,6 @@ var createCmd = &cobra.Command{
2353
2454 projectName := args [0 ]
2555
26- rootDir := fmt .Sprintf ("./%v" , projectName )
27-
28- log .Printf ("Creating project %s." , projectName )
29-
30- err := os .Mkdir (rootDir , os .ModePerm )
31- if os .IsExist (err ) {
32- log .Fatalf ("Directory %v already exists! Error: %v" , projectName , err )
33- } else if err != nil {
34- log .Fatalf ("Error creating root: %v " , err )
35- }
36-
37- commit0ConfigPath := fmt .Sprintf ("%v/commit0.yml" , rootDir )
38-
39- f , err := os .Create (commit0ConfigPath )
40- if err != nil {
41- log .Printf ("Error creating commit0 config: %v" , err )
42- }
43- Templator .Commit0 .Execute (f , projectName )
44-
45- gitIgnorePath := fmt .Sprintf ("%v/.gitignore" , rootDir )
46- f , err = os .Create (gitIgnorePath )
47- if err != nil {
48- log .Printf ("Error creating commit0 config: %v" , err )
49- }
50- Templator .GitIgnore .Execute (f , projectName )
56+ Create (projectName , "./" )
5157 },
5258}
0 commit comments