diff --git a/configs/viper.go b/configs/viper.go new file mode 100644 index 0000000..d5e4ede --- /dev/null +++ b/configs/viper.go @@ -0,0 +1,30 @@ +package configs + +import ( + "bytes" + "encoding/json" + "fmt" + "strings" + + "github.com/spf13/viper" +) + +func UnsetViperConfig(keys ...string) error { + configMap := viper.AllSettings() + for _, key := range keys { + delete(configMap, strings.ToLower(key)) + } + encodedConfig, err := json.MarshalIndent(configMap, "", " ") + if err != nil { + return err + } + err = viper.ReadConfig(bytes.NewReader(encodedConfig)) + if err != nil { + return err + } + err = viper.WriteConfig() + if err != nil { + return fmt.Errorf("Error removing configs: %w", err) + } + return nil +}