A config file manager for Go that uses a defaults and overrides file.
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.

50 lines
1.1 KiB

package yamlsettings
import (
"io"
"os"
"gopkg.in/yaml.v2"
)
// LoadFiles marshals your app settings from the default.yml overridden
// by the settings.yml file.
func LoadFiles(defaultsFile, settingsFile string, v interface{}) error {
// Read the defaults file.
if err := LoadFile(defaultsFile, v); err != nil {
return err
}
// Read the settings.yml. It's OK if it doesn't exist.
if err := LoadFile(settingsFile, v); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// LoadFile opens and marshals YAML settings from a single config file.
func LoadFile(filename string, v interface{}) error {
fh, err := os.Open(filename)
if err != nil {
return err
}
dec := yaml.NewDecoder(fh)
return dec.Decode(v)
}
// LoadReaders marshals your app settings from the defaults and settings
// readers, so you can feed in YAML data from sources other than files
// on disk.
func LoadReaders(defaults, settings io.Reader, v interface{}) error {
if defaults != nil {
if err := yaml.NewDecoder(defaults).Decode(v); err != nil {
return err
}
}
if settings != nil {
return yaml.NewDecoder(settings).Decode(v)
}
return nil
}