Basic information: I have created a go application and used Cobra for it. Cobra uses Viper for command line parameters and flags.
I have a command listen with a flag bind and I want to configure it in a yaml file.
Code:
The init function of the listen command looks like this:
func init() {
RootCmd.AddCommand(listenCmd)
listenCmd.Flags().StringP("bind", "b", ":50051", "Provide bind definition")
viper.BindPFlag("bind", listenCmd.Flags().Lookup("bind"))
}
Code of my application is at https://github.com/sascha-andres/go-logsink
Problem:
When I call the app with listen --bind "bla"
the flag is set correctly to bla
, but I have not found a way to achieve this using a YAML file located in my home directory.
Config files tried:
---
connect:
bind: "bla"
and
---
bind: "bla"
In both cases the config file was found but the flag had not the expected value but the default value.
How do I have to write the config file to have the flag populated correctly?
viper.GetString()
- and this question only has a link to repository which no longer has the code in question. – Tarbox