From abab9aa52a698bb6fcb945b10968695ec52379aa Mon Sep 17 00:00:00 2001 From: jharshman Date: Tue, 29 Jan 2019 19:34:11 -0800 Subject: [PATCH] vgo - add Create method to Project struct --- cobra/cmd/init.go | 62 +++----------------------------------------- cobra/cmd/project.go | 39 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 59 deletions(-) diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index c334121..2e5bc4e 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -15,14 +15,11 @@ package cmd import ( "fmt" - "github.com/spf13/cobra/cobra/tpl" + "github.com/spf13/cobra" + "github.com/spf13/viper" "os" "path" "path/filepath" - "text/template" - - "github.com/spf13/cobra" - "github.com/spf13/viper" ) var ( @@ -60,62 +57,9 @@ Init will not use an existing directory with contents.`, AppName: path.Base(pkgName), } - // create main.go - mainFile, err := os.Create(fmt.Sprintf("%s/main.go", project.AbsolutePath)) - if err != nil { + if err := project.Create(); err != nil { er(err) } - defer mainFile.Close() - - mainTemplate := template.Must(template.New("main").Parse(string(tpl.MainTemplate()))) - err = mainTemplate.Execute(mainFile, project) - if err != nil { - er(err) - } - - // create cmd/root.go - if _, err = os.Stat(fmt.Sprintf("%s/cmd", project.AbsolutePath)); os.IsNotExist(err) { - os.Mkdir("cmd", 0751) - } - rootFile, err := os.Create(fmt.Sprintf("%s/cmd/root.go", project.AbsolutePath)) - if err != nil { - er(err) - } - defer rootFile.Close() - - rootTemplate := template.Must(template.New("root").Parse(string(tpl.RootTemplate()))) - err = rootTemplate.Execute(rootFile, project) - if err != nil { - er(err) - } - - createLicenseFile(project.Legal, project.AbsolutePath) - - /* - wd, err := os.Getwd() - if err != nil { - er(err) - } - - var project *Project - if len(args) == 0 { - project = NewProjectFromPath(wd) - } else if len(args) == 1 { - arg := args[0] - if arg[0] == '.' { - arg = filepath.Join(wd, arg) - } - if filepath.IsAbs(arg) { - project = NewProjectFromPath(arg) - } else { - project = NewProject(arg) - } - } else { - er("please provide only one argument") - } - - initializeProject(project) - */ fmt.Printf("Your Cobra applicaton is ready at\n%s\n", project.AbsolutePath) }, diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index 3f55732..cf66e83 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -1,10 +1,13 @@ package cmd import ( + "fmt" + "github.com/spf13/cobra/cobra/tpl" "os" "path/filepath" "runtime" "strings" + "text/template" ) // Project contains name, license and paths to projects. @@ -25,6 +28,42 @@ type Project struct { name string } +func (p *Project) Create() error { + + // create main.go + mainFile, err := os.Create(fmt.Sprintf("%s/main.go", p.AbsolutePath)) + if err != nil { + return err + } + defer mainFile.Close() + + mainTemplate := template.Must(template.New("main").Parse(string(tpl.MainTemplate()))) + err = mainTemplate.Execute(mainFile, p) + if err != nil { + return err + } + + // create cmd/root.go + if _, err = os.Stat(fmt.Sprintf("%s/cmd", p.AbsolutePath)); os.IsNotExist(err) { + os.Mkdir("cmd", 0751) + } + rootFile, err := os.Create(fmt.Sprintf("%s/cmd/root.go", p.AbsolutePath)) + if err != nil { + return err + } + defer rootFile.Close() + + rootTemplate := template.Must(template.New("root").Parse(string(tpl.RootTemplate()))) + err = rootTemplate.Execute(rootFile, p) + if err != nil { + return err + } + + // create license + createLicenseFile(p.Legal, p.AbsolutePath) + return nil +} + // NewProject returns Project with specified project name. func NewProject(projectName string) *Project { if projectName == "" {