2019-04-24 21:52:08 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
2020-01-21 20:56:44 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/configuration/validator"
|
2020-01-21 20:56:44 +00:00
|
|
|
"github.com/spf13/viper"
|
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) {
|
2020-01-21 20:56:44 +00:00
|
|
|
viper.SetEnvPrefix("AUTHELIA")
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
viper.AutomaticEnv()
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
viper.SetConfigFile(configPath)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
return nil, []error{fmt.Errorf("unable to find config file %s", configPath)}
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
var configuration schema.Configuration
|
|
|
|
viper.Unmarshal(&configuration)
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
val := schema.NewStructValidator()
|
2020-01-21 20:56:44 +00:00
|
|
|
validator.Validate(&configuration, val)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
if val.HasErrors() {
|
|
|
|
return nil, val.Errors()
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:56:44 +00:00
|
|
|
return &configuration, nil
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|