808a1e9deb
Took way longer than it should had because I was researching exisiting options. As it turns out, nothing nice exists that would handle: - Multiple overridiable config files - Defaults provided in a struct - Output in a struct License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
35 lines
704 B
Go
35 lines
704 B
Go
package config
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
// FromFile loads config from a specified file overriding defaults specified in
|
|
// the source code. If file does not exist or is empty defaults are asummed.
|
|
func FromFile(path string) (*Root, error) {
|
|
file, err := os.Open(path)
|
|
switch {
|
|
case os.IsNotExist(err):
|
|
return Default(), nil
|
|
case err != nil:
|
|
return nil, err
|
|
}
|
|
|
|
defer file.Close() //nolint:errcheck // The file is RO
|
|
return FromReader(file)
|
|
}
|
|
|
|
// FromReader loads config from a reader instance.
|
|
func FromReader(reader io.Reader) (*Root, error) {
|
|
cfg := Default()
|
|
_, err := toml.DecodeReader(reader, cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|