Config: don't override undefined keys when loading from ENV

Composer can load configuration values defined as map from ENV.
Previously, when loading the configuration from ENV, the whole map would
get overridden, not just values defined in the ENV. This is however not
intended and not consistent with how loading configuration from file
works.

Adjust the configuration loading from ENV and adjust the unit test
accordingly.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
Tomáš Hozza 2024-07-17 10:01:36 +02:00 committed by Sanne Raymaekers
parent 62ae5aa4ea
commit 286236b698
2 changed files with 10 additions and 6 deletions

View file

@ -222,7 +222,11 @@ func loadConfigFromEnv(intf interface{}) error {
if err != nil {
return err
}
fieldV.Set(reflect.ValueOf(value))
// Don't override the whole map, just update the keys that are present in the env.
// This is consistent with how loading config from the file works.
for k, v := range value {
fieldV.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
case reflect.Struct:
err := loadConfigFromEnv(fieldV.Addr().Interface())
if err != nil {

View file

@ -170,12 +170,12 @@ func TestConfigFromEnv(t *testing.T) {
}
}()
os.Setenv("DISTRO_ALIASES", "rhel-7=rhel-7.9,rhel-8=rhel-8.9,rhel-9=rhel-9.3,rhel-10.0=rhel-9.5")
os.Setenv("DISTRO_ALIASES", "rhel-7=rhel-7.9,rhel-8=rhel-8.9,rhel-9=rhel-9.3")
expectedDistroAliases := map[string]string{
"rhel-7": "rhel-7.9",
"rhel-8": "rhel-8.9",
"rhel-9": "rhel-9.3",
"rhel-10.0": "rhel-9.5",
"rhel-7": "rhel-7.9",
"rhel-8": "rhel-8.9",
"rhel-9": "rhel-9.3",
"rhel-10": "rhel-10.0", // this value is from the default config
}
config, err := LoadConfig("")