From fb9b36f67787b597c1e888adaa107a7d66eb7f4d Mon Sep 17 00:00:00 2001 From: Wade Simmons Date: Wed, 26 Feb 2020 15:38:56 -0500 Subject: [PATCH] allow any config file name if specified directly (#189) Currently, we require that config file names end with `.yml` or `.yaml`. This is because if the user points `-config` at a directory of files, we only want to use the YAML files in that directory. But this makes it more difficult to use the `-test -config` option because config management tools might not have an extension on the file when preparing a new config file. This change makes it so that if you point `-config file` directly at a file, it uses it no matter what the extension is. --- config.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index e1ef5a3..7cf8037 100644 --- a/config.go +++ b/config.go @@ -35,7 +35,7 @@ func (c *Config) Load(path string) error { c.path = path c.files = make([]string, 0) - err := c.resolve(path) + err := c.resolve(path, true) if err != nil { return err } @@ -234,14 +234,16 @@ func (c *Config) get(k string, v interface{}) interface{} { return v } -func (c *Config) resolve(path string) error { +// direct signifies if this is the config path directly specified by the user, +// versus a file/dir found by recursing into that path +func (c *Config) resolve(path string, direct bool) error { i, err := os.Stat(path) if err != nil { return nil } if !i.IsDir() { - c.addFile(path) + c.addFile(path, direct) return nil } @@ -251,7 +253,7 @@ func (c *Config) resolve(path string) error { } for _, p := range paths { - err := c.resolve(filepath.Join(path, p)) + err := c.resolve(filepath.Join(path, p), false) if err != nil { return err } @@ -260,10 +262,10 @@ func (c *Config) resolve(path string) error { return nil } -func (c *Config) addFile(path string) error { +func (c *Config) addFile(path string, direct bool) error { ext := filepath.Ext(path) - if ext != ".yaml" && ext != ".yml" { + if !direct && ext != ".yaml" && ext != ".yml" { return nil }