Read config file from present working directory, if present

This commit is contained in:
Ben Johnson
2022-03-05 10:59:28 -07:00
parent 7fe79d3883
commit 8ee5fcb591

View File

@@ -217,13 +217,34 @@ func (c *Config) DBConfig(path string) *DBConfig {
// ReadConfigFile unmarshals config from filename. Expands path if needed. // ReadConfigFile unmarshals config from filename. Expands path if needed.
// If expandEnv is true then environment variables are expanded in the config. // If expandEnv is true then environment variables are expanded in the config.
// If filename is blank then the default config path is used. // If filename is blank then the default config path is used.
func ReadConfigFile(filename string, expandEnv bool) (_ Config, err error) { func ReadConfigFile(filename string, expandEnv bool) (config Config, err error) {
config := DefaultConfig() var filenames []string
if filename != "" {
useDefaultPath := filename == "" filenames = append(filenames, filename)
if useDefaultPath {
filename = DefaultConfigPath()
} }
filenames = append(filenames, "./litestream.yml")
filenames = append(filenames, DefaultConfigPath())
for _, name := range filenames {
isDefaultPath := name != filename
if config, err = readConfigFile(name, expandEnv); os.IsNotExist(err) {
if isDefaultPath {
continue
}
return config, fmt.Errorf("config file not found: %s", filename)
} else if err != nil {
return config, err
}
break
}
return config, nil
}
// readConfigFile unmarshals config from filename. Expands path if needed.
// If expandEnv is true then environment variables are expanded in the config.
func readConfigFile(filename string, expandEnv bool) (_ Config, err error) {
config := DefaultConfig()
// Expand filename, if necessary. // Expand filename, if necessary.
filename, err = expand(filename) filename, err = expand(filename)
@@ -234,12 +255,7 @@ func ReadConfigFile(filename string, expandEnv bool) (_ Config, err error) {
// Read configuration. // Read configuration.
// Do not return an error if using default path and file is missing. // Do not return an error if using default path and file is missing.
buf, err := ioutil.ReadFile(filename) buf, err := ioutil.ReadFile(filename)
if os.IsNotExist(err) { if err != nil {
if useDefaultPath {
return config, nil
}
return config, fmt.Errorf("config file not found: %s", filename)
} else if err != nil {
return config, err return config, err
} }