update doc w/ newer cmd/root.go example (#973)
This commit is contained in:
		
							
								
								
									
										45
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								README.md
									
									
									
									
									
								
							@ -210,33 +210,52 @@ You will additionally define flags and handle configuration in your init() funct
 | 
			
		||||
For example cmd/root.go:
 | 
			
		||||
 | 
			
		||||
```go
 | 
			
		||||
package cmd
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
  "os"
 | 
			
		||||
 | 
			
		||||
	homedir "github.com/mitchellh/go-homedir"
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var cfgFile string
 | 
			
		||||
var (
 | 
			
		||||
	// Used for flags.
 | 
			
		||||
	cfgFile     string
 | 
			
		||||
	userLicense string
 | 
			
		||||
 | 
			
		||||
	rootCmd = &cobra.Command{
 | 
			
		||||
		Use:   "cobra",
 | 
			
		||||
		Short: "A generator for Cobra based Applications",
 | 
			
		||||
		Long: `Cobra is a CLI library for Go that empowers applications.
 | 
			
		||||
This application is a tool to generate the needed files
 | 
			
		||||
to quickly create a Cobra application.`,
 | 
			
		||||
	}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Execute executes the root command.
 | 
			
		||||
func Execute() error {
 | 
			
		||||
	return rootCmd.Execute()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	cobra.OnInitialize(initConfig)
 | 
			
		||||
 | 
			
		||||
	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
 | 
			
		||||
  rootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")
 | 
			
		||||
  rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")
 | 
			
		||||
  rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `licensetext` in config)")
 | 
			
		||||
  rootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration")
 | 
			
		||||
	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")
 | 
			
		||||
	viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
 | 
			
		||||
  viper.BindPFlag("projectbase", rootCmd.PersistentFlags().Lookup("projectbase"))
 | 
			
		||||
	viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
 | 
			
		||||
	viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
 | 
			
		||||
	viper.SetDefault("license", "apache")
 | 
			
		||||
 | 
			
		||||
	rootCmd.AddCommand(addCmd)
 | 
			
		||||
	rootCmd.AddCommand(initCmd)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func initConfig() {
 | 
			
		||||
  // Don't forget to read config either from cfgFile or from home directory!
 | 
			
		||||
	if cfgFile != "" {
 | 
			
		||||
		// Use config file from the flag.
 | 
			
		||||
		viper.SetConfigFile(cfgFile)
 | 
			
		||||
@ -244,8 +263,7 @@ func initConfig() {
 | 
			
		||||
		// Find home directory.
 | 
			
		||||
		home, err := homedir.Dir()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
      fmt.Println(err)
 | 
			
		||||
      os.Exit(1)
 | 
			
		||||
			er(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Search config in home directory with name ".cobra" (without extension).
 | 
			
		||||
@ -253,9 +271,10 @@ func initConfig() {
 | 
			
		||||
		viper.SetConfigName(".cobra")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  if err := viper.ReadInConfig(); err != nil {
 | 
			
		||||
    fmt.Println("Can't read config:", err)
 | 
			
		||||
    os.Exit(1)
 | 
			
		||||
	viper.AutomaticEnv()
 | 
			
		||||
 | 
			
		||||
	if err := viper.ReadInConfig(); err == nil {
 | 
			
		||||
		fmt.Println("Using config file:", viper.ConfigFileUsed())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user