From bcc54ae73fff9cbaaca9eeff2f03861d1994ce6c Mon Sep 17 00:00:00 2001 From: Brian Meyers Date: Fri, 22 Feb 2019 13:06:52 -0500 Subject: [PATCH 1/2] Custom generator that will generate arguments and flags right from the command line --- cobra/cmd/add.go | 47 +++++++++++----------- cobra/cmd/flag_helpers.go | 84 +++++++++++++++++++++++++++++++++++++++ cobra/cmd/helpers.go | 2 +- cobra/cmd/test.go | 47 ---------------------- 4 files changed, 108 insertions(+), 72 deletions(-) create mode 100644 cobra/cmd/flag_helpers.go delete mode 100644 cobra/cmd/test.go diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index f5d3f17a..347eab76 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -25,9 +25,11 @@ import ( func init() { addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)") addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command") + addCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, "the arguments to auto generate format is 'flagName:type:description'") } var packageName, parentName string +var cmdFlags []string var addCmd = &cobra.Command{ Use: "add [command name]", @@ -69,7 +71,7 @@ Example: cobra add server -> resulting in a new cmd/server.go`, // validateCmdName returns source without any dashes and underscore. // If there will be dash or underscore, next letter will be uppered. // It supports only ASCII (1-byte character) strings. -// https://github.com/spf13/cobra/issues/269 +// https://github.com/OneCloudInc/cobra/issues/269 func validateCmdName(source string) string { i := 0 l := len(source) @@ -128,37 +130,25 @@ package {{.cmdPackage}} import ( "fmt" - "github.com/spf13/cobra" + "github.com/OneCloudInc/cobra" ) +{{ printFlagVars .flags }} + +func init() { + {{ printFlagCreates .flags }} + {{.parentName}}.AddCommand({{.cmdName}}Cmd) +} + // {{.cmdName}}Cmd represents the {{.cmdName}} command var {{.cmdName}}Cmd = &cobra.Command{ Use: "{{.cmdName}}", - Short: "A brief description of your command", - Long: ` + "`" + `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.` + "`" + `, + Short: "{{.cmdName}}", + Long: ` + "`" + `Description` + "`" + `, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("{{.cmdName}} called") + fmt.Println("{{.cmdName}} called, place the command logic here") }, } - -func init() { - {{.parentName}}.AddCommand({{.cmdName}}Cmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // {{.cmdName}}Cmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // {{.cmdName}}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} ` data := make(map[string]interface{}) @@ -167,6 +157,15 @@ func init() { data["cmdPackage"] = filepath.Base(filepath.Dir(path)) // last dir of path data["parentName"] = parentName data["cmdName"] = cmdName + flags := []flagDefinition{} + for _, value := range cmdFlags { + f, err := buildFlag(value, cmdName) + if err != nil { + er(err) + } + flags = append(flags, f) + } + data["flags"] = flags cmdScript, err := executeTemplate(template, data) if err != nil { diff --git a/cobra/cmd/flag_helpers.go b/cobra/cmd/flag_helpers.go new file mode 100644 index 00000000..89d35b9b --- /dev/null +++ b/cobra/cmd/flag_helpers.go @@ -0,0 +1,84 @@ +package cmd + +import ( + "errors" + "fmt" + "strings" +) + +var specialFlagTypes = []string{"stringMapFlag", "genericMapFlag"} + +// Indexes for flag definition string after split {name}:{type}:{description} +const ( + FlagDefLength = 3 + FlagDefSeparator = ":" + FlagDefault = ` "",` + FlagNameIndex = 0 + FlagTypeIndex = 1 + FlagDescriptionIndex = 2 +) + +type flagDefinition struct { + Name string + FlagType string + FlagDescription string + CreateFn string + VarName string + Default string + CmdName string +} + +func printFlagVars(iFlags interface{}) string { + flags := toFlagArray(iFlags) + varDefs := "" + for _, f := range flags { + varDefs += fmt.Sprintf("var %s %s\n", f.VarName, f.FlagType) + } + return varDefs +} + +func printFlagCreates(iFlags interface{}) string { + flags := toFlagArray(iFlags) + createStrs := "" + for _, f := range flags { + createStrs += fmt.Sprintf( `%sCmd.Flags().%s(&%s, "%s",%s "%s") + `, + f.CmdName, + f.CreateFn, + f.VarName, + f.Name, + f.Default, + f.FlagDescription) + } + return createStrs +} + +func toFlagArray(value interface{}) []flagDefinition { + switch v := value.(type) { + case []flagDefinition: + return v + // Add whatever other types you need + default: + return []flagDefinition{} + } +} + +func buildFlag(in, cmdName string) (flagDefinition, error) { + var def flagDefinition + values := strings.Split(in, FlagDefSeparator) + if len(values) != FlagDefLength { + return def, errors.New("invalid flag definition, make sure to specify flags as 'name:type:definition' they are all required") + } + name, flagType, desc := values[FlagNameIndex], values[FlagTypeIndex],values[FlagDescriptionIndex] + def = flagDefinition{ + Name: name, + FlagType: flagType, + FlagDescription: desc, + CreateFn: fmt.Sprintf("%s%s", strings.Title(flagType), "Var"), + VarName: fmt.Sprintf("%s%sFlag", cmdName, name), + Default:FlagDefault, + CmdName: cmdName, + } + + return def, nil +} diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index cd94b3e3..c946839a 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -111,7 +111,7 @@ func exists(path string) bool { } func executeTemplate(tmplStr string, data interface{}) (string, error) { - tmpl, err := template.New("").Funcs(template.FuncMap{"comment": commentifyString}).Parse(tmplStr) + tmpl, err := template.New("").Funcs(template.FuncMap{"comment": commentifyString, "printFlagVars": printFlagVars, "printFlagCreates": printFlagCreates}).Parse(tmplStr) if err != nil { return "", err } diff --git a/cobra/cmd/test.go b/cobra/cmd/test.go deleted file mode 100644 index 98e24872..00000000 --- a/cobra/cmd/test.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright © 2019 NAME HERE -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cmd - -import ( - "fmt" - - "github.com/OneCloudInc/cobra" -) - -var testqueryIdFlag string -var testparamsFlag stringMap - - -func init() { - testCmd.Flags().StringVar(&testqueryIdFlag, "queryId", "", "Query ID") -testCmd.Flags().StringMapVar(&testparamsFlag, "params", "", "Params") - - rootCmd.AddCommand(testCmd) -} - -// testCmd represents the test command -var testCmd = &cobra.Command{ - Use: "test", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("test called") - }, -} From b3a45b8407085eb3e7d79a44363f2d3496bf21dd Mon Sep 17 00:00:00 2001 From: Brian Meyers Date: Fri, 22 Feb 2019 13:19:46 -0500 Subject: [PATCH 2/2] Allow both persistent and local flags for root vs command. Removed default viper. Allow flags to be set on the rootCmd as well. --- cobra/cmd/add.go | 4 ++-- cobra/cmd/flag_helpers.go | 9 ++++++-- cobra/cmd/init.go | 47 +++++++++++++++++++++++++++------------ cobra/cmd/root.go | 2 +- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 347eab76..8620465a 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -25,7 +25,7 @@ import ( func init() { addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)") addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command") - addCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, "the arguments to auto generate format is 'flagName:type:description'") + addCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, `the flags to auto generate. For each flag do '--flag "flagName:type:description"'`) } var packageName, parentName string @@ -136,7 +136,7 @@ import ( {{ printFlagVars .flags }} func init() { - {{ printFlagCreates .flags }} + {{ printFlagCreates .flags false }} {{.parentName}}.AddCommand({{.cmdName}}Cmd) } diff --git a/cobra/cmd/flag_helpers.go b/cobra/cmd/flag_helpers.go index 89d35b9b..9120be94 100644 --- a/cobra/cmd/flag_helpers.go +++ b/cobra/cmd/flag_helpers.go @@ -37,13 +37,18 @@ func printFlagVars(iFlags interface{}) string { return varDefs } -func printFlagCreates(iFlags interface{}) string { +func printFlagCreates(iFlags interface{}, persistent bool) string { flags := toFlagArray(iFlags) createStrs := "" + flagsFn := "Flags" + if persistent { + flagsFn = "PersistentFlags" + } for _, f := range flags { - createStrs += fmt.Sprintf( `%sCmd.Flags().%s(&%s, "%s",%s "%s") + createStrs += fmt.Sprintf( `%sCmd.%s().%s(&%s, "%s",%s "%s") `, f.CmdName, + flagsFn, f.CreateFn, f.VarName, f.Name, diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index 5b795a13..27baf31e 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -23,6 +23,10 @@ import ( "github.com/spf13/viper" ) +func init() { + initCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, `the flags to auto generate. For each flag do '--flag "flagName:type:description"'`) +} + var initCmd = &cobra.Command{ Use: "init [name]", Aliases: []string{"initialize", "initialise", "create"}, @@ -150,6 +154,24 @@ import ( var cfgFile string{{end}} +{{ printFlagVars .flags }} + +func init() { {{- if .viper}} + cobra.OnInitialize(initConfig) +{{end}} + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application.{{ if .viper }} + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }} + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }} + + // Cobra also supports local flags, which will only run + // when this action is called directly. + + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + {{ printFlagCreates .flags true }} +} + // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "{{.appName}}", @@ -173,20 +195,7 @@ func Execute() { os.Exit(1) } } - -func init() { {{- if .viper}} - cobra.OnInitialize(initConfig) -{{end}} - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application.{{ if .viper }} - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }} - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }} - - // Cobra also supports local flags, which will only run - // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -}{{ if .viper }} +{{ if .viper }} // initConfig reads in config file and ENV variables if set. func initConfig() { @@ -221,6 +230,16 @@ func initConfig() { data["license"] = project.License().Header data["appName"] = path.Base(project.Name()) + flags := []flagDefinition{} + for _, value := range cmdFlags { + f, err := buildFlag(value, "rootCmd") + if err != nil { + er(err) + } + flags = append(flags, f) + } + data["flags"] = flags + rootCmdScript, err := executeTemplate(template, data) if err != nil { er(err) diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index f63fef52..39dbf008 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -45,7 +45,7 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution") rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project") - rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration") + rootCmd.PersistentFlags().Bool("viper", false, "use Viper for configuration") viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")) viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")) viper.SetDefault("author", "NAME HERE ")