2019-04-24 21:52:08 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/configuration/validator"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func check(e error) {
|
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a YAML configuration and create a Configuration object out of it.
|
|
|
|
func Read(configPath string) (*schema.Configuration, []error) {
|
|
|
|
config := schema.Configuration{}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(configPath)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
err = yaml.Unmarshal([]byte(data), &config)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
val := schema.NewStructValidator()
|
|
|
|
validator.Validate(&config, val)
|
|
|
|
|
|
|
|
if val.HasErrors() {
|
|
|
|
return nil, val.Errors()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|