vgo-support - re-working code generator
This commit is contained in:
		@ -15,19 +15,24 @@ 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 (
 | 
				
			||||||
	Use:     "init [name]",
 | 
						pkgName string
 | 
				
			||||||
	Aliases: []string{"initialize", "initialise", "create"},
 | 
					
 | 
				
			||||||
	Short:   "Initialize a Cobra Application",
 | 
						initCmd = &cobra.Command{
 | 
				
			||||||
	Long: `Initialize (cobra init) will create a new application, with a license
 | 
							Use:     "init [name]",
 | 
				
			||||||
 | 
							Aliases: []string{"initialize", "initialise", "create"},
 | 
				
			||||||
 | 
							Short:   "Initialize a Cobra Application",
 | 
				
			||||||
 | 
							Long: `Initialize (cobra init) will create a new application, with a license
 | 
				
			||||||
and the appropriate structure for a Cobra-based CLI application.
 | 
					and the appropriate structure for a Cobra-based CLI application.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  * If a name is provided, it will be created in the current directory;
 | 
					  * If a name is provided, it will be created in the current directory;
 | 
				
			||||||
@ -39,37 +44,66 @@ 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)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		var project *Project
 | 
								wd, err := os.Getwd()
 | 
				
			||||||
		if len(args) == 0 {
 | 
								if err != nil {
 | 
				
			||||||
			project = NewProjectFromPath(wd)
 | 
									er(err)
 | 
				
			||||||
		} else if len(args) == 1 {
 | 
					 | 
				
			||||||
			arg := args[0]
 | 
					 | 
				
			||||||
			if arg[0] == '.' {
 | 
					 | 
				
			||||||
				arg = filepath.Join(wd, arg)
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if filepath.IsAbs(arg) {
 | 
					
 | 
				
			||||||
				project = NewProjectFromPath(arg)
 | 
								project := &Project{
 | 
				
			||||||
			} else {
 | 
									AbsolutePath: wd,
 | 
				
			||||||
				project = NewProject(arg)
 | 
									PkgName:      pkgName,
 | 
				
			||||||
 | 
									Legal:        getLicense(),
 | 
				
			||||||
 | 
									Copyright:    copyrightLine(),
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			er("please provide only one argument")
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		initializeProject(project)
 | 
								// open file for writing
 | 
				
			||||||
 | 
								f, err := os.Create(fmt.Sprintf("%s/main.go", project.AbsolutePath))
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									er(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								defer f.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		fmt.Fprintln(cmd.OutOrStdout(), `Your Cobra application is ready at
 | 
								t := template.Must(template.New("init").Parse(string(tpl.MainTemplate())))
 | 
				
			||||||
`+project.AbsPath()+`
 | 
								err = t.Execute(f, project)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									er(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								/*
 | 
				
			||||||
 | 
									wd, err := os.Getwd()
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										er(err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Give it a try by going there and running `+"`go run main.go`."+`
 | 
									var project *Project
 | 
				
			||||||
Add commands to it by running `+"`cobra add [cmdname]`.")
 | 
									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)
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						initCmd.Flags().StringVar(&pkgName, "pkg-name", "", "fully qualified pkg name")
 | 
				
			||||||
 | 
						initCmd.MarkFlagRequired("pkg-name")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func initializeProject(project *Project) {
 | 
					func initializeProject(project *Project) {
 | 
				
			||||||
 | 
				
			|||||||
@ -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
 | 
				
			||||||
 | 
				
			|||||||
@ -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
									
								
							
							
						
						
									
										17
									
								
								cobra/tpl/main.go
									
									
									
									
									
										Normal 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()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					`)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user