Skip to content

Commit

Permalink
vgo - add Create method to Project struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jharshman authored and spf13 committed Jun 7, 2019
1 parent 69420a9 commit abab9aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 59 deletions.
62 changes: 3 additions & 59 deletions cobra/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -60,63 +57,10 @@ 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 {
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 {
if err := project.Create(); 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)
},
}
Expand Down
39 changes: 39 additions & 0 deletions cobra/cmd/project.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 == "" {
Expand Down

0 comments on commit abab9aa

Please sign in to comment.