Boolean env vars should support all truthy values.

Update config precedence code to allow baseline defaults for keys with no corresponding flags.
Fix default values od database-port and database-listen.
Closes #35
This commit is contained in:
kai
2021-03-18 10:44:20 +00:00
parent 886084b7ec
commit dfc35cf637
10 changed files with 84 additions and 74 deletions

View File

@@ -9,7 +9,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/go-kit/types"
"github.com/turbot/steampipe-plugin-sdk/logging"
"github.com/turbot/steampipe/cmdconfig"
"github.com/turbot/steampipe/constants"
@@ -72,8 +71,8 @@ func initGlobalConfig() {
utils.FailOnError(err)
steampipeconfig.Config = config
// todo set viper config from config
setViperDefaults(steampipeconfig.Config)
// set viper config defaults from config and env vars
cmdconfig.SetViperDefaults(steampipeconfig.Config)
}
// CreateLogger :: create a hclog logger with the level specified by the SP_LOG env var
@@ -102,36 +101,6 @@ func setInstallDir() {
constants.SteampipeDir = installDir
}
func setViperDefaults(config *steampipeconfig.SteampipeConfig) {
setViperDefaultsFromConfig(config)
overrideViperDefaultsFromEnv()
}
func setViperDefaultsFromConfig(config *steampipeconfig.SteampipeConfig) {
for k, v := range config.ConfigMap() {
viper.SetDefault(k, v)
}
}
func overrideViperDefaultsFromEnv() {
// a map of known environment variables to map to viper keys
ingest := map[string]string{
constants.ENV_UPDATE_CHECK: constants.ArgUpdateCheck,
}
for k, v := range ingest {
if val, ok := os.LookupEnv(k); ok {
// if the env val is one of known acceptable booleans e.g : on/off - yes/no etc,
// then we take the boolean value
if boolVal, err := types.ToBool(val); err != nil {
viper.SetDefault(v, val)
} else {
// otherwise, use it as is
viper.SetDefault(v, boolVal)
}
}
}
}
func AddCommands() {
// explicitly initialise commands here rather than in init functions to allow us to handle errors from the config load
rootCmd.AddCommand(PluginCmd())