Add prefix to env variable lookup, replace . with _

e.g. $PACKWIZ_INIT_REINIT is equivalent to the -r flag
This commit is contained in:
comp500 2023-07-06 04:56:58 +01:00
parent 99d0af14c4
commit beef8c6692

View File

@ -6,6 +6,7 @@ import (
"github.com/spf13/pflag" "github.com/spf13/pflag"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -92,7 +93,10 @@ func initConfig() {
viper.SetConfigName(".packwiz") viper.SetConfigName(".packwiz")
} }
viper.AutomaticEnv() // read in environment variables that match // Read in environment variables that match
viper.SetEnvPrefix("packwiz")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
// If a config file is found, read it in. // If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil { if err := viper.ReadInConfig(); err == nil {