Skip to content
Myr

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

5 files+31 added−37 removed

modified:cobra/cmd/testdata/root.go.golden+5 added-5 removed
23 unmodified lines
24)2526var cfgFile string2728// RootCmd represents the base command when called without any subcommands29var RootCmd = &cobra.Command{28// rootCmd represents the base command when called without any subcommands29var 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 contains33examples and usage of using your application. For example:
8 unmodified lines
4243// 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.45func 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 lines
5455 // 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)")5960 // 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}6465// initConfig reads in config file and ENV variables if set.66func initConfig() {
23 unmodified lines
modified:cobra/cmd/testdata/test.go.golden+1 added-1 removed
34 unmodified lines
35 },36}3738func init() {39 RootCmd.AddCommand(testCmd)39 rootCmd.AddCommand(testCmd)4041 // Here you will define your flags and configuration settings.4243 // Cobra supports Persistent Flags which will work for this command
7 unmodified lines
modified:cobra/cmd/add.go+2 added-2 removed
22 unmodified lines
23)2425func 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}2930var packageName, parentName string31
2 unmodified lines
34 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 and37the appropriate structure for a Cobra-based CLI application,38and register it to its parent (default RootCmd).38and register it to its parent (default rootCmd).3940If you want your command to be public, pass in the command name41with an initial uppercase letter.42
137 unmodified lines
modified:cobra/cmd/init.go+6 added-6 removed
148 unmodified lines
149){{if .viper}}150151var cfgFile string{{end}}152153// RootCmd represents the base command when called without any subcommands154var RootCmd = &cobra.Command{153// rootCmd represents the base command when called without any subcommands154var 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 contains158examples and usage of using your application. For example:
8 unmodified lines
167168// 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.170func 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 lines
179{{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 }}185186 // 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 }}190191// initConfig reads in config file and ENV variables if set.192func initConfig() {
42 unmodified lines
modified:README.md+17 added-23 removed
156 unmodified lines
157 "{pathToYourApp}/cmd"158)159160func main() {161 if err := cmd.RootCmd.Execute(); err != nil {162 fmt.Println(err)163 os.Exit(1)164 }161 cmd.Execute()162}163```164165## Using the Cobra Generator
4 unmodified lines
170[Here](https://github.com/spf13/cobra/blob/master/cobra/README.md) you can find more information about it.171172## Using the Cobra Library173177To manually implement Cobra you need to create a bare main.go file and a RootCmd file.174To manually implement Cobra you need to create a bare main.go file and a rootCmd file.175You will optionally provide additional commands as you see fit.176177### Create rootCmd178
1 unmodified line
180181Ideally you place this in app/cmd/root.go:182183```go187var RootCmd = &cobra.Command{184var 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 lines
208)209210func 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 lines
263 "{pathToYourApp}/cmd"264)265266func main() {270 if err := cmd.RootCmd.Execute(); err != nil {271 fmt.Println(err)272 os.Exit(1)273 }267 cmd.Execute()268}269```270271### Create additional commands
13 unmodified lines
285 "github.com/spf13/cobra"286)287288func init() {295 RootCmd.AddCommand(versionCmd)289 rootCmd.AddCommand(versionCmd)290}291292var versionCmd = &cobra.Command{293 Use: "version",
28 unmodified lines
322command it's assigned to as well as every command under that command. For323global flags, assign a flag as a persistent flag on the root.324325```go332RootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")326rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")327```328329### Local Flags330331A flag can also be assigned locally which will only apply to that specific command.332333```go340RootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")334rootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")335```336337### Local Flag on Parent Commands338
14 unmodified lines
353```go354var author string355356func 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```361362In this example the persistent flag `author` is bound with `viper`.
350 unmodified lines