2017-04-29 10:02:02 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-01-30 03:34:11 +00:00
|
|
|
"fmt"
|
2017-04-29 10:02:02 +00:00
|
|
|
"os"
|
2019-01-30 03:34:11 +00:00
|
|
|
"text/template"
|
2019-07-30 22:26:11 +00:00
|
|
|
|
2021-02-08 00:08:50 +00:00
|
|
|
"github.com/spf13/cobra"
|
2019-07-30 22:26:11 +00:00
|
|
|
"github.com/spf13/cobra/cobra/tpl"
|
2017-04-29 10:02:02 +00:00
|
|
|
)
|
|
|
|
|
2017-04-29 12:53:52 +00:00
|
|
|
// Project contains name, license and paths to projects.
|
2017-04-29 10:02:02 +00:00
|
|
|
type Project struct {
|
2019-01-29 08:46:54 +00:00
|
|
|
// v2
|
|
|
|
PkgName string
|
|
|
|
Copyright string
|
|
|
|
AbsolutePath string
|
|
|
|
Legal License
|
2019-01-29 09:19:08 +00:00
|
|
|
Viper bool
|
|
|
|
AppName string
|
2017-04-29 10:02:02 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 09:24:26 +00:00
|
|
|
type Command struct {
|
|
|
|
CmdName string
|
|
|
|
CmdParent string
|
|
|
|
*Project
|
|
|
|
}
|
|
|
|
|
2019-01-30 03:34:11 +00:00
|
|
|
func (p *Project) Create() error {
|
2019-01-30 08:20:25 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 03:34:11 +00:00
|
|
|
// 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) {
|
2021-02-08 00:08:50 +00:00
|
|
|
cobra.CheckErr(os.Mkdir(fmt.Sprintf("%s/cmd", p.AbsolutePath), 0751))
|
2019-01-30 03:34:11 +00:00
|
|
|
}
|
|
|
|
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
|
2019-01-30 04:28:47 +00:00
|
|
|
return p.createLicenseFile()
|
2019-01-30 03:34:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 04:25:38 +00:00
|
|
|
func (p *Project) createLicenseFile() error {
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"copyright": copyrightLine(),
|
|
|
|
}
|
|
|
|
licenseFile, err := os.Create(fmt.Sprintf("%s/LICENSE", p.AbsolutePath))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-17 15:02:42 +00:00
|
|
|
defer licenseFile.Close()
|
2019-01-30 04:25:38 +00:00
|
|
|
|
|
|
|
licenseTemplate := template.Must(template.New("license").Parse(p.Legal.Text))
|
|
|
|
return licenseTemplate.Execute(licenseFile, data)
|
|
|
|
}
|
|
|
|
|
2019-01-30 09:24:26 +00:00
|
|
|
func (c *Command) Create() error {
|
2019-01-31 03:47:38 +00:00
|
|
|
cmdFile, err := os.Create(fmt.Sprintf("%s/cmd/%s.go", c.AbsolutePath, c.CmdName))
|
2019-01-30 09:33:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer cmdFile.Close()
|
|
|
|
|
|
|
|
commandTemplate := template.Must(template.New("sub").Parse(string(tpl.AddCommandTemplate())))
|
2019-01-30 09:44:39 +00:00
|
|
|
err = commandTemplate.Execute(cmdFile, c)
|
2019-01-30 09:33:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-30 09:44:39 +00:00
|
|
|
return nil
|
2019-01-30 09:24:26 +00:00
|
|
|
}
|