Make rootCmd private
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Update Cobra generator to make rootCmd private rather than exporting it. Also update examples in README to use the exported Execute() command rather than referencing unexported rootCmd. Fixes #556
authored by Nick Miyake · committed by Albert Nigmatzianov · parent fb02817 · Browse files · 5 files·+31 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+31 added−37 removed
- other:+31 added−37 removed
Provenance
- Commit
1be1d28- Tree
6731814- Author
Nick Miyake <nmiyake@palantir.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
fb02817
5 files changed+31 added−37 removed
modified:cobra/cmd/testdata/root.go.golden+5 added-5 removed
23 unmodified lines24 )25 26 var cfgFile string27 28-// RootCmd represents the base command when called without any subcommands29-var RootCmd = &cobra.Command{28+// rootCmd represents the base command when called without any subcommands29+var rootCmd = &cobra.Command{30 Use: "testproject",31 Short: "A brief description of your application",32 Long: `A longer description that spans multiple lines and likely contains33 examples and usage of using your application. For example:8 unmodified lines42 43 // Execute adds all child commands to the root command and sets flags appropriately.44 // This is called by main.main(). It only needs to happen once to the rootCmd.45 func Execute() {46- if err := RootCmd.Execute(); err != nil {46+ if err := rootCmd.Execute(); err != nil {47 fmt.Println(err)48 os.Exit(1)49 }50 }3 unmodified lines54 55 // Here you will define your flags and configuration settings.56 // Cobra supports persistent flags, which, if defined here,57 // will be global for your application.58- RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)")58+ rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)")59 60 // Cobra also supports local flags, which will only run61 // when this action is called directly.62- RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")62+ rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")63 }64 65 // initConfig reads in config file and ENV variables if set.66 func initConfig() {23 unmodified linesmodified:cobra/cmd/testdata/test.go.golden+1 added-1 removed
34 unmodified lines35 },36 }37 38 func init() {39- RootCmd.AddCommand(testCmd)39+ rootCmd.AddCommand(testCmd)40 41 // Here you will define your flags and configuration settings.42 43 // Cobra supports Persistent Flags which will work for this command7 unmodified linesmodified:cobra/cmd/add.go+2 added-2 removed
22 unmodified lines23 )24 25 func init() {26 addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)")27- addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "variable name of parent command for this command")27+ addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command")28 }29 30 var packageName, parentName string31 2 unmodified lines34 Aliases: []string{"command"},35 Short: "Add a command to a Cobra Application",36 Long: `Add (cobra add) will create a new command, with a license and37 the appropriate structure for a Cobra-based CLI application,38-and register it to its parent (default RootCmd).38+and register it to its parent (default rootCmd).39 40 If you want your command to be public, pass in the command name41 with an initial uppercase letter.42 137 unmodified linesmodified:cobra/cmd/init.go+6 added-6 removed
148 unmodified lines149 ){{if .viper}}150 151 var cfgFile string{{end}}152 153-// RootCmd represents the base command when called without any subcommands154-var RootCmd = &cobra.Command{153+// rootCmd represents the base command when called without any subcommands154+var rootCmd = &cobra.Command{155 Use: "{{.appName}}",156 Short: "A brief description of your application",157 Long: ` + "`" + `A longer description that spans multiple lines and likely contains158 examples and usage of using your application. For example:8 unmodified lines167 168 // Execute adds all child commands to the root command and sets flags appropriately.169 // This is called by main.main(). It only needs to happen once to the rootCmd.170 func Execute() {171- if err := RootCmd.Execute(); err != nil {171+ if err := rootCmd.Execute(); err != nil {172 fmt.Println(err)173 os.Exit(1)174 }175 }3 unmodified lines179 {{end}}180 // Here you will define your flags and configuration settings.181 // Cobra supports persistent flags, which, if defined here,182 // will be global for your application.{{ if .viper }}183- RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }}184- // RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }}183+ rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }}184+ // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }}185 186 // Cobra also supports local flags, which will only run187 // when this action is called directly.188- RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")188+ rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")189 }{{ if .viper }}190 191 // initConfig reads in config file and ENV variables if set.192 func initConfig() {42 unmodified linesmodified:README.md+17 added-23 removed
156 unmodified lines157 "{pathToYourApp}/cmd"158 )159 160 func main() {161- if err := cmd.RootCmd.Execute(); err != nil {162- fmt.Println(err)163- os.Exit(1)164- }161+ cmd.Execute()162 }163 ```164 165 ## Using the Cobra Generator4 unmodified lines170 [Here](https://github.com/spf13/cobra/blob/master/cobra/README.md) you can find more information about it.171 172 ## Using the Cobra Library173 177-To manually implement Cobra you need to create a bare main.go file and a RootCmd file.174+To manually implement Cobra you need to create a bare main.go file and a rootCmd file.175 You will optionally provide additional commands as you see fit.176 177 ### Create rootCmd178 1 unmodified line180 181 Ideally you place this in app/cmd/root.go:182 183 ```go187-var RootCmd = &cobra.Command{184+var rootCmd = &cobra.Command{185 Use: "hugo",186 Short: "Hugo is a very fast static site generator",187 Long: `A Fast and Flexible Static Site Generator built with188 love by spf13 and friends in Go.19 unmodified lines208 )209 210 func init() {211 cobra.OnInitialize(initConfig)215- RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")216- RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")217- RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")218- RootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")219- RootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")220- viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))221- viper.BindPFlag("projectbase", RootCmd.PersistentFlags().Lookup("projectbase"))222- viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper"))212+ rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")213+ rootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")214+ rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")215+ rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")216+ rootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")217+ viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))218+ viper.BindPFlag("projectbase", rootCmd.PersistentFlags().Lookup("projectbase"))219+ viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))220 viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")221 viper.SetDefault("license", "apache")222 }223 39 unmodified lines263 "{pathToYourApp}/cmd"264 )265 266 func main() {270- if err := cmd.RootCmd.Execute(); err != nil {271- fmt.Println(err)272- os.Exit(1)273- }267+ cmd.Execute()268 }269 ```270 271 ### Create additional commands13 unmodified lines285 "github.com/spf13/cobra"286 )287 288 func init() {295- RootCmd.AddCommand(versionCmd)289+ rootCmd.AddCommand(versionCmd)290 }291 292 var versionCmd = &cobra.Command{293 Use: "version",28 unmodified lines322 command it's assigned to as well as every command under that command. For323 global flags, assign a flag as a persistent flag on the root.324 325 ```go332-RootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")326+rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")327 ```328 329 ### Local Flags330 331 A flag can also be assigned locally which will only apply to that specific command.332 333 ```go340-RootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")334+rootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")335 ```336 337 ### Local Flag on Parent Commands338 14 unmodified lines353 ```go354 var author string355 356 func init() {363- RootCmd.PersistentFlags().StringVar(&author, "author", "YOUR NAME", "Author name for copyright attribution")364- viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))357+ rootCmd.PersistentFlags().StringVar(&author, "author", "YOUR NAME", "Author name for copyright attribution")358+ viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))359 }360 ```361 362 In this example the persistent flag `author` is bound with `viper`.350 unmodified lines