feat: more load options

This commit is contained in:
Mikaël Cluseau 2018-07-07 10:08:42 +11:00
parent a44d1cbe7f
commit 852aa03280

View File

@ -2,12 +2,54 @@ package config
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
// Load a config from a file.
func Load(file string) (config *Config, err error) {
f, err := os.Open(file)
if err != nil {
return
}
defer f.Close()
config, err = Read(f)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %v", file, err)
}
return
}
// Read a config from a reader.
func Read(reader io.Reader) (config *Config, err error) {
config = &Config{}
err = yaml.NewDecoder(reader).Decode(config)
if err != nil {
return nil, err
}
return
}
// Parse the config in data.
func Parse(data []byte) (config *Config, err error) {
config = &Config{}
err = yaml.Unmarshal(data, config)
if err != nil {
return nil, err
}
return
}
// Config represent this system's configuration // Config represent this system's configuration
type Config struct { type Config struct {
Vars []VarDefault Vars []VarDefault
@ -83,19 +125,3 @@ type NetworkDef struct {
Optional bool Optional bool
Script string Script string
} }
func Load(file string) (config *Config, err error) {
config = &Config{}
configData, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %v", file, err)
}
err = yaml.Unmarshal(configData, config)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %v", file, err)
}
return
}