From e993d53002aa6dca9d85ed6d8633a6984c3eafb1 Mon Sep 17 00:00:00 2001 From: jharshman Date: Wed, 30 Jan 2019 00:20:25 -0800 Subject: [PATCH] vgo - take named directory or current wd --- cobra/cmd/init.go | 8 +++++--- cobra/cmd/project.go | 10 +++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) 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 {