Add explanation for viper.BindPflag in README.md

Fix #429

/cc @deltaskelta
This commit is contained in:
Albert Nigmatzianov 2017-05-09 18:23:36 +02:00
parent ca57f0f5db
commit 7918bf6886

View file

@ -276,7 +276,6 @@ You will optionally provide additional commands as you see fit.
The root command represents your binary itself. The root command represents your binary itself.
#### Manually create rootCmd #### Manually create rootCmd
Cobra doesn't require any special constructors. Simply create your commands. Cobra doesn't require any special constructors. Simply create your commands.
@ -298,9 +297,18 @@ var RootCmd = &cobra.Command{
You will additionally define flags and handle configuration in your init() function. You will additionally define flags and handle configuration in your init() function.
for example cmd/root.go: For example cmd/root.go:
```go ```go
import (
"fmt"
"os"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
@ -314,6 +322,30 @@ func init() {
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>") viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "apache") viper.SetDefault("license", "apache")
} }
func main() {
// Don't forget to read config either from cfgFile or from home directory!
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(home)
os.Exit(1)
}
// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".cobra")
}
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Can't read config:", err)
os.Exit(1)
}
}
``` ```
### Create your main.go ### Create your main.go
@ -341,7 +373,6 @@ func main() {
} }
``` ```
### Create additional commands ### Create additional commands
Additional commands can be defined and typically are each given their own file Additional commands can be defined and typically are each given their own file
@ -431,6 +462,23 @@ A flag can also be assigned locally which will only apply to that specific comma
RootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from") RootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")
``` ```
### Bind Flags with Config
You can also bind your flags with [viper](https://github.com/spf13/viper):
```go
var author string
func init() {
RootCmd.PersistentFlags().StringVar(&author, "author", "YOUR NAME", "Author name for copyright attribution")
viper.BindPFlag("author", RootCmd.PersistentFlags().Lookup("author"))
}
```
In this example the persistent flag `author` is bound with `viper`.
**Note**, that the variable `author` will not be set to the value from config,
when the `--author` flag is not provided by user.
More in [viper documentation](https://github.com/spf13/viper#working-with-flags).
## Example ## Example