You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
714 B
42 lines
714 B
package config
|
|
|
|
import (
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
// Config is application configuration.
|
|
type Config struct {
|
|
DB struct {
|
|
FileName string `yaml:"file_name"`
|
|
} `yaml:"db"`
|
|
|
|
Server struct {
|
|
Address string `yaml:"address"`
|
|
} `yaml:"server"`
|
|
}
|
|
|
|
// Load loads the first valid config file from the list of file paths.
|
|
func Load(filePaths ...string) (Config, error) {
|
|
file, err := os.Open(filePaths[0])
|
|
if err != nil {
|
|
if len(filePaths) > 1 {
|
|
return Load(filePaths[1:]...)
|
|
}
|
|
|
|
return Config{}, err
|
|
}
|
|
|
|
config := Config{}
|
|
err = yaml.NewDecoder(file).Decode(&config)
|
|
if err != nil {
|
|
if len(filePaths) > 1 {
|
|
return Load(filePaths[1:]...)
|
|
}
|
|
|
|
return Config{}, err
|
|
}
|
|
|
|
return config, nil
|
|
}
|