I'm working on a Cobra-based app in Go and for one of my commands I'm defining a flag like this:
func init() {
myCmd.Flags().StringVarP(&project, "project", "p", "defaultProject", "Project name")
}
I also have a Viper set up in my root command that reads some configuration from a file. Is there a way to set the default value of the flag with Viper? I've tried this:
func init() {
myCmd.Flags().StringVarP(&project, "project", "p", viper.GetString("project"), "Project name")
}
But it seems this is not the way, as I'm getting an empty string if I omit the flag when calling the command. Any ideas? Thanks in advance!