Add explanation for viper.BindPflag in README.md
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #429 /cc @deltaskelta
authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent ca57f0f · Browse files · 1 file·+51 added−3 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+51 added−3 removed
- other:+51 added−3 removed
Provenance
- Commit
7918bf6- Tree
f19486c- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
ca57f0f
1 file changed+51 added−3 removed
modified:README.md+51 added-3 removed
274 unmodified lines275 ### Create the root command276 277 The root command represents your binary itself.278 279-279 #### Manually create rootCmd280 281 Cobra doesn't require any special constructors. Simply create your commands.282 13 unmodified lines296 ```297 298 You will additionally define flags and handle configuration in your init() function.299 301-for example cmd/root.go:300+For example cmd/root.go:301 302 ```go303+import (304+ "fmt"305+ "os"306+307+ homedir "github.com/mitchellh/go-homedir"308+ "github.com/spf13/cobra"309+ "github.com/spf13/viper"310+)311+312 func init() {313 cobra.OnInitialize(initConfig)314 RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")315 RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory eg. github.com/spf13/")5 unmodified lines321 viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper"))322 viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")323 viper.SetDefault("license", "apache")324 }325+326+func main() {327+ // Don't forget to read config either from cfgFile or from home directory!328+ if cfgFile != "" {329+ // Use config file from the flag.330+ viper.SetConfigFile(cfgFile)331+ } else {332+ // Find home directory.333+ home, err := homedir.Dir()334+ if err != nil {335+ fmt.Println(home)336+ os.Exit(1)337+ }338+339+ // Search config in home directory with name ".cobra" (without extension).340+ viper.AddConfigPath(home)341+ viper.SetConfigName(".cobra")342+ }343+344+ if err := viper.ReadInConfig(); err != nil {345+ fmt.Println("Can't read config:", err)346+ os.Exit(1)347+ }348+}349 ```350 351 ### Create your main.go352 19 unmodified lines372 }373 }374 ```375 344-376 ### Create additional commands377 378 Additional commands can be defined and typically are each given their own file379 inside of the cmd/ directory.81 unmodified lines461 ```go462 RootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")463 ```464 465+### Bind Flags with Config466 467+You can also bind your flags with [viper](https://github.com/spf13/viper):468+```go469+var author string470+471+func init() {472+ RootCmd.PersistentFlags().StringVar(&author, "author", "YOUR NAME", "Author name for copyright attribution")473+ viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))474+}475+```476+477+In this example the persistent flag `author` is bound with `viper`.478+**Note**, that the variable `author` will not be set to the value from config,479+when the `--author` flag is not provided by user.480+481+More in [viper documentation](https://github.com/spf13/viper#working-with-flags).482+483 ## Example484 485 In the example below, we have defined three commands. Two are at the top level486 and one (cmdTimes) is a child of one of the top commands. In this case the root449 unmodified lines