Using spf13/viper to gather parameters from various sources can be tricky. You will probably run into the problem of having to redefine default parameters for each means of input. This example shows how to to load parameters from different sources without having to define them multiple times. The common priority for input methods is this:
- CLI parameters
- Environment variables
- Config file
- Default values
Checkout the cli package on how to setup this config.
Build the application
make buildParameters can be passed into the application in these 4 ways:
config.yaml:
param1: "param from config"defaultConfig in config.go:
var defaultConfig = config{
Param2: "param with default value",
}Environment variable
PARAM1="param from env" ./our_appCli parameter
./our_app --param1="param from cli"