Skip to content

Commit 48e6ac4

Browse files
authored
update doc w/ newer cmd/root.go example (spf13#973)
1 parent 3745fcd commit 48e6ac4

File tree

1 file changed

+57
-38
lines changed

1 file changed

+57
-38
lines changed

README.md

+57-38
Original file line numberDiff line numberDiff line change
@@ -210,53 +210,72 @@ You will additionally define flags and handle configuration in your init() funct
210210
For example cmd/root.go:
211211

212212
```go
213+
package cmd
214+
213215
import (
214-
"fmt"
215-
"os"
216+
"fmt"
216217

217-
homedir "github.com/mitchellh/go-homedir"
218-
"github.com/spf13/cobra"
219-
"github.com/spf13/viper"
218+
homedir "github.com/mitchellh/go-homedir"
219+
"github.com/spf13/cobra"
220+
"github.com/spf13/viper"
220221
)
221222

222-
var cfgFile string
223+
var (
224+
// Used for flags.
225+
cfgFile string
226+
userLicense string
227+
228+
rootCmd = &cobra.Command{
229+
Use: "cobra",
230+
Short: "A generator for Cobra based Applications",
231+
Long: `Cobra is a CLI library for Go that empowers applications.
232+
This application is a tool to generate the needed files
233+
to quickly create a Cobra application.`,
234+
}
235+
)
236+
237+
// Execute executes the root command.
238+
func Execute() error {
239+
return rootCmd.Execute()
240+
}
223241

224242
func init() {
225-
cobra.OnInitialize(initConfig)
226-
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
227-
rootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")
228-
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")
229-
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")
230-
rootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")
231-
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
232-
viper.BindPFlag("projectbase", rootCmd.PersistentFlags().Lookup("projectbase"))
233-
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
234-
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
235-
viper.SetDefault("license", "apache")
243+
cobra.OnInitialize(initConfig)
244+
245+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
246+
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
247+
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
248+
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
249+
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
250+
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
251+
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
252+
viper.SetDefault("license", "apache")
253+
254+
rootCmd.AddCommand(addCmd)
255+
rootCmd.AddCommand(initCmd)
236256
}
237257

238258
func initConfig() {
239-
// Don't forget to read config either from cfgFile or from home directory!
240-
if cfgFile != "" {
241-
// Use config file from the flag.
242-
viper.SetConfigFile(cfgFile)
243-
} else {
244-
// Find home directory.
245-
home, err := homedir.Dir()
246-
if err != nil {
247-
fmt.Println(err)
248-
os.Exit(1)
249-
}
250-
251-
// Search config in home directory with name ".cobra" (without extension).
252-
viper.AddConfigPath(home)
253-
viper.SetConfigName(".cobra")
254-
}
255-
256-
if err := viper.ReadInConfig(); err != nil {
257-
fmt.Println("Can't read config:", err)
258-
os.Exit(1)
259-
}
259+
if cfgFile != "" {
260+
// Use config file from the flag.
261+
viper.SetConfigFile(cfgFile)
262+
} else {
263+
// Find home directory.
264+
home, err := homedir.Dir()
265+
if err != nil {
266+
er(err)
267+
}
268+
269+
// Search config in home directory with name ".cobra" (without extension).
270+
viper.AddConfigPath(home)
271+
viper.SetConfigName(".cobra")
272+
}
273+
274+
viper.AutomaticEnv()
275+
276+
if err := viper.ReadInConfig(); err == nil {
277+
fmt.Println("Using config file:", viper.ConfigFileUsed())
278+
}
260279
}
261280
```
262281

0 commit comments

Comments
 (0)