Skip empty config values when merging config files
This commit is contained in:
@@ -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 != "" {
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user