From 9e38de62f8e75610afbde914f132d83f1e6769cc Mon Sep 17 00:00:00 2001 From: Maina Wycliffe Date: Wed, 11 Dec 2019 02:17:22 +0300 Subject: [PATCH] feat: add a config remover --- configs/viper.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 configs/viper.go 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 +}