From bcc54ae73fff9cbaaca9eeff2f03861d1994ce6c Mon Sep 17 00:00:00 2001 From: Brian Meyers Date: Fri, 22 Feb 2019 13:06:52 -0500 Subject: [PATCH] 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") - }, -}