From 286236b698c5f3d0bdaa00258c45266bd021078c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Wed, 17 Jul 2024 10:01:36 +0200 Subject: [PATCH] Config: don't override undefined keys when loading from ENV MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- cmd/osbuild-composer/config.go | 6 +++++- cmd/osbuild-composer/config_test.go | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/osbuild-composer/config.go b/cmd/osbuild-composer/config.go index 176eed508..cd38999b2 100644 --- a/cmd/osbuild-composer/config.go +++ b/cmd/osbuild-composer/config.go @@ -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 { diff --git a/cmd/osbuild-composer/config_test.go b/cmd/osbuild-composer/config_test.go index ba7377527..2e7e9992d 100644 --- a/cmd/osbuild-composer/config_test.go +++ b/cmd/osbuild-composer/config_test.go @@ -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("")