vgo - add Create method to Project struct

This commit is contained in:
jharshman 2019-01-29 19:34:11 -08:00 committed by Steve Francia
parent 69420a9ffa
commit abab9aa52a
2 changed files with 42 additions and 59 deletions

View File

@ -15,14 +15,11 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/spf13/cobra/cobra/tpl" "github.com/spf13/cobra"
"github.com/spf13/viper"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"text/template"
"github.com/spf13/cobra"
"github.com/spf13/viper"
) )
var ( var (
@ -60,62 +57,9 @@ Init will not use an existing directory with contents.`,
AppName: path.Base(pkgName), AppName: path.Base(pkgName),
} }
// create main.go if err := project.Create(); err != nil {
mainFile, err := os.Create(fmt.Sprintf("%s/main.go", project.AbsolutePath))
if err != nil {
er(err) 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) fmt.Printf("Your Cobra applicaton is ready at\n%s\n", project.AbsolutePath)
}, },

View File

@ -1,10 +1,13 @@
package cmd package cmd
import ( import (
"fmt"
"github.com/spf13/cobra/cobra/tpl"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"text/template"
) )
// Project contains name, license and paths to projects. // Project contains name, license and paths to projects.
@ -25,6 +28,42 @@ type Project struct {
name string 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. // NewProject returns Project with specified project name.
func NewProject(projectName string) *Project { func NewProject(projectName string) *Project {
if projectName == "" { if projectName == "" {