Understanding the Synergy Between Cobra and Viper
Discover how to combine Cobra's command-line argument parsing with Viper's configuration management. Understand how to bind Cobra flags to Viper, allowing your Go command-line programs to handle configuration from multiple sources, including flags, environment variables, and files.
We'll cover the following...
Cobra and Viper were designed by the same author in order to work together. The integration is very simple and smooth.
Overview
As you recall, Cobra has fantastic support for parsing command-line arguments, but it doesn’t offer any built-in capabilities for managing any other type of configuration. This is all by design to let Cobra focus on the command-line and rely on Viper for all other forms of configuration.
Cobra and Viper
Command-line arguments or flags are different from all other forms of configuration because they are not used just for configuration. When you run a command-line program and provide it some command-line arguments, some of the arguments will be the command itself and possibly some sub-commands, some other arguments may be configuration flags and yet other arguments may be inputs. ...