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.
This commit is contained in:
Wade Simmons 2020-02-26 15:38:56 -05:00 committed by GitHub
parent 4d1928f1e3
commit fb9b36f677
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 6 deletions

View File

@ -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
}