Allow both persistent and local flags for root vs command. Removed default viper. Allow flags to be set on the rootCmd as well.

This commit is contained in:
Brian Meyers 2019-02-22 13:19:46 -05:00
parent bcc54ae73f
commit b3a45b8407
4 changed files with 43 additions and 19 deletions

View file

@ -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)
}

View file

@ -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,

View file

@ -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)

View file

@ -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 <EMAIL ADDRESS>")