vgo-support - re-working code generator

This commit is contained in:
jharshman 2019-01-29 00:46:54 -08:00 committed by Steve Francia
parent d658160bdd
commit 80ea2901b6
4 changed files with 90 additions and 31 deletions

View File

@ -15,15 +15,20 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/spf13/cobra/cobra/tpl"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"text/template"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var initCmd = &cobra.Command{ var (
pkgName string
initCmd = &cobra.Command{
Use: "init [name]", Use: "init [name]",
Aliases: []string{"initialize", "initialise", "create"}, Aliases: []string{"initialize", "initialise", "create"},
Short: "Initialize a Cobra Application", Short: "Initialize a Cobra Application",
@ -40,6 +45,32 @@ and the appropriate structure for a Cobra-based CLI application.
Init will not use an existing directory with contents.`, Init will not use an existing directory with contents.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
wd, err := os.Getwd()
if err != nil {
er(err)
}
project := &Project{
AbsolutePath: wd,
PkgName: pkgName,
Legal: getLicense(),
Copyright: copyrightLine(),
}
// open file for writing
f, err := os.Create(fmt.Sprintf("%s/main.go", project.AbsolutePath))
if err != nil {
er(err)
}
defer f.Close()
t := template.Must(template.New("init").Parse(string(tpl.MainTemplate())))
err = t.Execute(f, project)
if err != nil {
er(err)
}
/*
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
er(err) er(err)
@ -63,14 +94,17 @@ Init will not use an existing directory with contents.`,
} }
initializeProject(project) initializeProject(project)
*/
fmt.Fprintln(cmd.OutOrStdout(), `Your Cobra application is ready at fmt.Printf("Your Cobra applicaton is ready at\n%s\n", project.AbsolutePath)
`+project.AbsPath()+`
Give it a try by going there and running `+"`go run main.go`."+`
Add commands to it by running `+"`cobra add [cmdname]`.")
}, },
} }
)
func init() {
initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")
initCmd.MarkFlagRequired("pkg-name")
}
func initializeProject(project *Project) { func initializeProject(project *Project) {
if !exists(project.AbsPath()) { // If path doesn't yet exist, create it if !exists(project.AbsPath()) { // If path doesn't yet exist, create it

View File

@ -9,6 +9,13 @@ import (
// Project contains name, license and paths to projects. // Project contains name, license and paths to projects.
type Project struct { type Project struct {
// v2
PkgName string
Copyright string
AbsolutePath string
Legal License
// v1
absPath string absPath string
cmdPath string cmdPath string
srcPath string srcPath string

View File

@ -23,7 +23,8 @@ import (
var ( var (
// Used for flags. // Used for flags.
cfgFile, userLicense string cfgFile string
userLicense string
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "cobra", Use: "cobra",

17
cobra/tpl/main.go Normal file
View File

@ -0,0 +1,17 @@
package tpl
func MainTemplate() []byte {
return []byte(`
/*
{{ .Copyright }}
{{if .Legal.Header}}{{ .Legal.Header }}{{end}}
*/
package main
import "{{ .PkgName }}/cmd"
func main() {
cmd.Execute()
}
`)
}