mirror of
https://github.com/spf13/cobra
synced 2025-05-05 21:07:24 +00:00
Merge pull request #1 from OneCloudInc/feature-oc-generator
Feature oc generator
This commit is contained in:
commit
65c085b0b3
6 changed files with 147 additions and 87 deletions
|
@ -25,9 +25,11 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)")
|
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().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command")
|
||||||
|
addCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, `the flags to auto generate. For each flag do '--flag "flagName:type:description"'`)
|
||||||
}
|
}
|
||||||
|
|
||||||
var packageName, parentName string
|
var packageName, parentName string
|
||||||
|
var cmdFlags []string
|
||||||
|
|
||||||
var addCmd = &cobra.Command{
|
var addCmd = &cobra.Command{
|
||||||
Use: "add [command name]",
|
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.
|
// validateCmdName returns source without any dashes and underscore.
|
||||||
// If there will be dash or underscore, next letter will be uppered.
|
// If there will be dash or underscore, next letter will be uppered.
|
||||||
// It supports only ASCII (1-byte character) strings.
|
// 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 {
|
func validateCmdName(source string) string {
|
||||||
i := 0
|
i := 0
|
||||||
l := len(source)
|
l := len(source)
|
||||||
|
@ -128,37 +130,25 @@ package {{.cmdPackage}}
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/OneCloudInc/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
{{ printFlagVars .flags }}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
{{ printFlagCreates .flags false }}
|
||||||
|
{{.parentName}}.AddCommand({{.cmdName}}Cmd)
|
||||||
|
}
|
||||||
|
|
||||||
// {{.cmdName}}Cmd represents the {{.cmdName}} command
|
// {{.cmdName}}Cmd represents the {{.cmdName}} command
|
||||||
var {{.cmdName}}Cmd = &cobra.Command{
|
var {{.cmdName}}Cmd = &cobra.Command{
|
||||||
Use: "{{.cmdName}}",
|
Use: "{{.cmdName}}",
|
||||||
Short: "A brief description of your command",
|
Short: "{{.cmdName}}",
|
||||||
Long: ` + "`" + `A longer description that spans multiple lines and likely contains examples
|
Long: ` + "`" + `Description` + "`" + `,
|
||||||
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) {
|
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{})
|
data := make(map[string]interface{})
|
||||||
|
@ -167,6 +157,15 @@ func init() {
|
||||||
data["cmdPackage"] = filepath.Base(filepath.Dir(path)) // last dir of path
|
data["cmdPackage"] = filepath.Base(filepath.Dir(path)) // last dir of path
|
||||||
data["parentName"] = parentName
|
data["parentName"] = parentName
|
||||||
data["cmdName"] = cmdName
|
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)
|
cmdScript, err := executeTemplate(template, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
89
cobra/cmd/flag_helpers.go
Normal file
89
cobra/cmd/flag_helpers.go
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
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{}, persistent bool) string {
|
||||||
|
flags := toFlagArray(iFlags)
|
||||||
|
createStrs := ""
|
||||||
|
flagsFn := "Flags"
|
||||||
|
if persistent {
|
||||||
|
flagsFn = "PersistentFlags"
|
||||||
|
}
|
||||||
|
for _, f := range flags {
|
||||||
|
createStrs += fmt.Sprintf( `%sCmd.%s().%s(&%s, "%s",%s "%s")
|
||||||
|
`,
|
||||||
|
f.CmdName,
|
||||||
|
flagsFn,
|
||||||
|
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
|
||||||
|
}
|
|
@ -111,7 +111,7 @@ func exists(path string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeTemplate(tmplStr string, data interface{}) (string, error) {
|
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,10 @@ import (
|
||||||
"github.com/spf13/viper"
|
"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{
|
var initCmd = &cobra.Command{
|
||||||
Use: "init [name]",
|
Use: "init [name]",
|
||||||
Aliases: []string{"initialize", "initialise", "create"},
|
Aliases: []string{"initialize", "initialise", "create"},
|
||||||
|
@ -150,6 +154,24 @@ import (
|
||||||
|
|
||||||
var cfgFile string{{end}}
|
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
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "{{.appName}}",
|
Use: "{{.appName}}",
|
||||||
|
@ -173,20 +195,7 @@ func Execute() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
{{ if .viper }}
|
||||||
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 }}
|
|
||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
@ -221,6 +230,16 @@ func initConfig() {
|
||||||
data["license"] = project.License().Header
|
data["license"] = project.License().Header
|
||||||
data["appName"] = path.Base(project.Name())
|
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)
|
rootCmdScript, err := executeTemplate(template, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
|
|
|
@ -45,7 +45,7 @@ func init() {
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
|
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().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
|
||||||
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
|
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("author", rootCmd.PersistentFlags().Lookup("author"))
|
||||||
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
||||||
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
|
|
||||||
//
|
|
||||||
// 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")
|
|
||||||
},
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue