update doc w/ newer cmd/root.go example (#973)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Joshua Harshman · committed by GitHub · parent 3745fcd · Browse files · 1 file·+55 added−36 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+55 added−36 removed
- other:+55 added−36 removed
Provenance
- Commit
48e6ac4- Tree
f717ac1- Author
Joshua Harshman <josh.harshman@bit.ly>- Committer
GitHub <noreply@github.com>- Parent
3745fcd
1 file changed+55 added−36 removed
modified:README.md+55 added-36 removed
208 unmodified lines209 210 For example cmd/root.go:211 212 ```go213+package cmd214+215 import (214- "fmt"215- "os"216+ "fmt"217 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"221 )222 223+var (224+ // Used for flags.222-var cfgFile string225+ cfgFile string226+ userLicense string227 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 files233+to quickly create a Cobra application.`,234+ }235+)236+237+// Execute executes the root command.238+func Execute() error {239+ return rootCmd.Execute()240+}241+242 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)256 }257 258 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)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+ }249- }268 251- // Search config in home directory with name ".cobra" (without extension).252- viper.AddConfigPath(home)253- viper.SetConfigName(".cobra")254- }269+ // Search config in home directory with name ".cobra" (without extension).270+ viper.AddConfigPath(home)271+ viper.SetConfigName(".cobra")272+ }273 256- if err := viper.ReadInConfig(); err != nil {257- fmt.Println("Can't read config:", err)258- os.Exit(1)259- }274+ viper.AutomaticEnv()275+276+ if err := viper.ReadInConfig(); err == nil {277+ fmt.Println("Using config file:", viper.ConfigFileUsed())278+ }279 }280 ```281 282 ### Create your main.go482 unmodified lines