update doc w/ newer cmd/root.go example (#973)

This commit is contained in:
Joshua Harshman 2019-10-07 15:31:10 -06:00 committed by GitHub
parent 3745fcd262
commit 48e6ac4718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 37 deletions

View File

@ -210,53 +210,72 @@ You will additionally define flags and handle configuration in your init() funct
For example cmd/root.go: For example cmd/root.go:
```go ```go
import ( package cmd
"fmt"
"os"
homedir "github.com/mitchellh/go-homedir" import (
"github.com/spf13/cobra" "fmt"
"github.com/spf13/viper"
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() { func init() {
cobra.OnInitialize(initConfig) 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().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution") 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().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
rootCmd.PersistentFlags().Bool("viper", true, "Use Viper for configuration") rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")) viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
viper.BindPFlag("projectbase", rootCmd.PersistentFlags().Lookup("projectbase")) viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")) viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>") viper.SetDefault("license", "apache")
viper.SetDefault("license", "apache")
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(initCmd)
} }
func initConfig() { func initConfig() {
// Don't forget to read config either from cfgFile or from home directory! if cfgFile != "" {
if cfgFile != "" { // Use config file from the flag.
// Use config file from the flag. viper.SetConfigFile(cfgFile)
viper.SetConfigFile(cfgFile) } else {
} else { // Find home directory.
// Find home directory. home, err := homedir.Dir()
home, err := homedir.Dir() if err != nil {
if err != nil { er(err)
fmt.Println(err) }
os.Exit(1)
}
// Search config in home directory with name ".cobra" (without extension). // Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(home) viper.AddConfigPath(home)
viper.SetConfigName(".cobra") viper.SetConfigName(".cobra")
} }
if err := viper.ReadInConfig(); err != nil { viper.AutomaticEnv()
fmt.Println("Can't read config:", err)
os.Exit(1) if err := viper.ReadInConfig(); err == nil {
} fmt.Println("Using config file:", viper.ConfigFileUsed())
}
} }
``` ```