diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index a6abd1c..63397d1 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -47,9 +47,11 @@ Init will not use an existing directory with contents.`, er(err) } - // todo: - // if . use current directory - // else create named directory and set wd to that + if len(args) > 0 { + if args[0] != "." { + wd = fmt.Sprintf("%s/%s", wd, args[0]) + } + } project := &Project{ AbsolutePath: wd, diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index 47c63ab..34dea56 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -30,6 +30,14 @@ type Project struct { func (p *Project) Create() error { + // check if AbsolutePath exists + if _, err := os.Stat(p.AbsolutePath); os.IsNotExist(err) { + // create directory + if err := os.Mkdir(p.AbsolutePath, 0754); err != nil { + return err + } + } + // create main.go mainFile, err := os.Create(fmt.Sprintf("%s/main.go", p.AbsolutePath)) if err != nil { @@ -45,7 +53,7 @@ func (p *Project) Create() error { // create cmd/root.go if _, err = os.Stat(fmt.Sprintf("%s/cmd", p.AbsolutePath)); os.IsNotExist(err) { - os.Mkdir("cmd", 0751) + os.Mkdir(fmt.Sprintf("%s/cmd", p.AbsolutePath), 0751) } rootFile, err := os.Create(fmt.Sprintf("%s/cmd/root.go", p.AbsolutePath)) if err != nil {