From 7bb41d2f215f7f07b21858445cff6ebf72b3ee7b Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Tue, 29 Sep 2015 15:07:41 -0700 Subject: [PATCH] agent: return empty config for empty config dirs --- command/agent/config.go | 5 +++++ command/agent/config_test.go | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/command/agent/config.go b/command/agent/config.go index c39aa0dcb..22fd36bc5 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -561,6 +561,11 @@ func LoadConfigDir(dir string) (*Config, error) { } } + // Fast-path if we have no files + if len(files) == 0 { + return &Config{}, nil + } + var result *Config for _, f := range files { config, err := LoadConfigFile(f) diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 3c6f6d38f..cf5cb2127 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -170,6 +170,15 @@ func TestConfig_LoadConfigDir(t *testing.T) { } defer os.RemoveAll(dir) + // Returns empty config on empty dir + config, err := LoadConfig(dir) + if err != nil { + t.Fatalf("err: %s", err) + } + if config == nil { + t.Fatalf("should not be nil") + } + file1 := filepath.Join(dir, "conf1.hcl") err = ioutil.WriteFile(file1, []byte(`{"region":"west"}`), 0600) if err != nil { @@ -198,7 +207,7 @@ func TestConfig_LoadConfigDir(t *testing.T) { } // Works if configs are valid - config, err := LoadConfigDir(dir) + config, err = LoadConfigDir(dir) if err != nil { t.Fatalf("err: %s", err) }