From e46e390c57d5b401abc004221cdba46dab001d42 Mon Sep 17 00:00:00 2001 From: wesleymccollam Date: Wed, 23 Apr 2025 14:57:38 -0400 Subject: [PATCH 1/2] support original Viper keys --- internal/profiles/koanf.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/internal/profiles/koanf.go b/internal/profiles/koanf.go index 61dbc165..74b09977 100644 --- a/internal/profiles/koanf.go +++ b/internal/profiles/koanf.go @@ -233,6 +233,36 @@ func (k KoanfConfig) GetProfileKoanf(pName string) (subKoanf *koanf.Koanf, err e } func (k KoanfConfig) WriteFile() (err error) { + // TODO - Remove this for loop prior to v0.7.0 release + for _, profileName := range k.ProfileNames() { + for key, val := range k.KoanfInstance().All() { + if profileName == key || !strings.Contains(key, profileName) { + continue + } + for _, opt := range options.Options() { + fullKoanfKeyValue := fmt.Sprintf("%s.%s", profileName, opt.KoanfKey) + if fullKoanfKeyValue == key { + continue + } + if strings.ToLower(fullKoanfKeyValue) == key { + err = k.KoanfInstance().Set(fullKoanfKeyValue, val) + if err != nil { + return fmt.Errorf("error setting koanf key %s: %w", fullKoanfKeyValue, err) + } + k.KoanfInstance().Delete(key) + } + } + } + } + + // TODO - Remove this originalActiveProfileKey logic prior to v0.7.0 release + // Delete the original active profile key if it exists + // and the new active profile key if it exists + originalActiveProfileKey := strings.ToLower(options.RootActiveProfileOption.KoanfKey) + if k.KoanfInstance().Exists(originalActiveProfileKey) && k.KoanfInstance().Exists(options.RootActiveProfileOption.KoanfKey) { + k.KoanfInstance().Delete(strings.ToLower(originalActiveProfileKey)) + } + encodedConfig, err := k.KoanfInstance().Marshal(yaml.Parser()) if err != nil { return fmt.Errorf("error marshalling koanf: %w", err) From bce367d313e6c0b7ee766a6c101b3177e24d47d0 Mon Sep 17 00:00:00 2001 From: wesleymccollam Date: Wed, 23 Apr 2025 15:12:17 -0400 Subject: [PATCH 2/2] update comment --- internal/profiles/koanf.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/profiles/koanf.go b/internal/profiles/koanf.go index 74b09977..d39a3644 100644 --- a/internal/profiles/koanf.go +++ b/internal/profiles/koanf.go @@ -256,8 +256,7 @@ func (k KoanfConfig) WriteFile() (err error) { } // TODO - Remove this originalActiveProfileKey logic prior to v0.7.0 release - // Delete the original active profile key if it exists - // and the new active profile key if it exists + // Delete the original active profile key if it exists and the new activeProfile exists originalActiveProfileKey := strings.ToLower(options.RootActiveProfileOption.KoanfKey) if k.KoanfInstance().Exists(originalActiveProfileKey) && k.KoanfInstance().Exists(options.RootActiveProfileOption.KoanfKey) { k.KoanfInstance().Delete(strings.ToLower(originalActiveProfileKey))