I’m working with Cobra and Viper to create a little command line program, and I’m trying to figure out how to access the config file Viper reads in subcommands. Right now, I’m marshalling the YAML config into a global struct, which works, but I’m wondering if there is a better way to access the config elements. I’ve looked around, but it’s not clear to me how to accomplish this.
My example config is in toml because it’s what I have handy. Viper is agnostic to the config format. A configuration struct is not required, Viper handles this for you.
value := viper.GetString("section.key") works in the subcommand after viper is imported.
It wasn’t clear that viper instances would inherit the config loaded in the root file. I was thinking I needed to access the viper config through cobra some how.