diff --git a/internal/puppet/config.go b/internal/puppet/config.go index fa9be8a..9895313 100644 --- a/internal/puppet/config.go +++ b/internal/puppet/config.go @@ -75,7 +75,8 @@ func ConfigPath() string { } // Load merges, lowest precedence first: the built-in role fact, the system -// config file, the user config file, then NODE_LOOKUP_* env vars. Flags are +// config file, the user config file, then NODE_LOOKUP_* env vars; empty values +// never override. Flags are // applied by the caller, which then calls Require. func Load() (Config, error) { cfg := Config{RoleFact: DefaultRoleFact} @@ -87,9 +88,15 @@ func Load() (Config, error) { if err != nil { return cfg, fmt.Errorf("reading config %s: %w", path, err) } - if err := yaml.Unmarshal(data, &cfg); err != nil { + var file Config + if err := yaml.Unmarshal(data, &file); err != nil { return cfg, fmt.Errorf("parsing config %s: %w", path, err) } + for key := range envVars { + if v := *file.field(key); v != "" { + *cfg.field(key) = v + } + } } for key, env := range envVars { if v := os.Getenv(env); v != "" { diff --git a/internal/puppet/puppet_test.go b/internal/puppet/puppet_test.go index 3a2eff2..1ac6004 100644 --- a/internal/puppet/puppet_test.go +++ b/internal/puppet/puppet_test.go @@ -68,6 +68,21 @@ func TestLoad_Precedence(t *testing.T) { } } +func TestLoad_EmptyUserValuesKeepSystemValues(t *testing.T) { + system, user := isolate(t) + writeFile(t, system, "puppetdb_url: http://system\npuppetboard_url: https://system\ndomain: system.example.com\n") + writeFile(t, user, "puppetdb_url: \"\"\npuppetboard_url: \"\"\ndomain: \"\"\nrole_fact: enc_role\n") + + cfg, err := Load() + if err != nil { + t.Fatal(err) + } + want := Config{PuppetDBURL: "http://system", PuppetboardURL: "https://system", Domain: "system.example.com", RoleFact: DefaultRoleFact} + if cfg != want { + t.Fatalf("got %+v, want %+v", cfg, want) + } +} + func TestLoad_InvalidYAMLNamesPath(t *testing.T) { system, _ := isolate(t) writeFile(t, system, ":\tinvalid: yaml:\n")