Skip to content

Commit 08d068e

Browse files
support original Viper keys in config file (#101)
1 parent c5ef339 commit 08d068e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

internal/profiles/koanf.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,35 @@ func (k KoanfConfig) GetProfileKoanf(pName string) (subKoanf *koanf.Koanf, err e
233233
}
234234

235235
func (k KoanfConfig) WriteFile() (err error) {
236+
// TODO - Remove this for loop prior to v0.7.0 release
237+
for _, profileName := range k.ProfileNames() {
238+
for key, val := range k.KoanfInstance().All() {
239+
if profileName == key || !strings.Contains(key, profileName) {
240+
continue
241+
}
242+
for _, opt := range options.Options() {
243+
fullKoanfKeyValue := fmt.Sprintf("%s.%s", profileName, opt.KoanfKey)
244+
if fullKoanfKeyValue == key {
245+
continue
246+
}
247+
if strings.ToLower(fullKoanfKeyValue) == key {
248+
err = k.KoanfInstance().Set(fullKoanfKeyValue, val)
249+
if err != nil {
250+
return fmt.Errorf("error setting koanf key %s: %w", fullKoanfKeyValue, err)
251+
}
252+
k.KoanfInstance().Delete(key)
253+
}
254+
}
255+
}
256+
}
257+
258+
// TODO - Remove this originalActiveProfileKey logic prior to v0.7.0 release
259+
// Delete the original active profile key if it exists and the new activeProfile exists
260+
originalActiveProfileKey := strings.ToLower(options.RootActiveProfileOption.KoanfKey)
261+
if k.KoanfInstance().Exists(originalActiveProfileKey) && k.KoanfInstance().Exists(options.RootActiveProfileOption.KoanfKey) {
262+
k.KoanfInstance().Delete(strings.ToLower(originalActiveProfileKey))
263+
}
264+
236265
encodedConfig, err := k.KoanfInstance().Marshal(yaml.Parser())
237266
if err != nil {
238267
return fmt.Errorf("error marshalling koanf: %w", err)

0 commit comments

Comments
 (0)