123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- package configor
- import (
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "path"
- "reflect"
- "strings"
- "time"
- "github.com/BurntSushi/toml"
- "gopkg.in/yaml.v2"
- )
- // UnmatchedTomlKeysError errors are returned by the Load function when
- // ErrorOnUnmatchedKeys is set to true and there are unmatched keys in the input
- // toml config file. The string returned by Error() contains the names of the
- // missing keys.
- type UnmatchedTomlKeysError struct {
- Keys []toml.Key
- }
- func (e *UnmatchedTomlKeysError) Error() string {
- return fmt.Sprintf("There are keys in the config file that do not match any field in the given struct: %v", e.Keys)
- }
- func (configor *Configor) getENVPrefix(config interface{}) string {
- if configor.Config.ENVPrefix == "" {
- if prefix := os.Getenv("CONFIGOR_ENV_PREFIX"); prefix != "" {
- return prefix
- }
- return "Configor"
- }
- return configor.Config.ENVPrefix
- }
- func getConfigurationFileWithENVPrefix(file, env string) (string, time.Time, error) {
- var (
- envFile string
- extname = path.Ext(file)
- )
- if extname == "" {
- envFile = fmt.Sprintf("%v.%v", file, env)
- } else {
- envFile = fmt.Sprintf("%v.%v%v", strings.TrimSuffix(file, extname), env, extname)
- }
- if fileInfo, err := os.Stat(envFile); err == nil && fileInfo.Mode().IsRegular() {
- return envFile, fileInfo.ModTime(), nil
- }
- return "", time.Now(), fmt.Errorf("failed to find file %v", file)
- }
- func (configor *Configor) getConfigurationFiles(watchMode bool, files ...string) ([]string, map[string]time.Time, []error) {
- var resultKeys []string
- var results = map[string]time.Time{}
- var resultsErrors []error = make([]error, 0, len(files))
- if !watchMode && (configor.Config.Debug || configor.Config.Verbose) {
- fmt.Printf("Current environment: '%v'\n", configor.GetEnvironment())
- }
- for i := len(files) - 1; i >= 0; i-- {
- foundFile := false
- file := files[i]
- // check configuration
- if fileInfo, err := os.Stat(file); err == nil && fileInfo.Mode().IsRegular() {
- foundFile = true
- resultKeys = append(resultKeys, file)
- results[file] = fileInfo.ModTime()
- }
- // check configuration with env
- if file, modTime, err := getConfigurationFileWithENVPrefix(file, configor.GetEnvironment()); err == nil {
- foundFile = true
- resultKeys = append(resultKeys, file)
- results[file] = modTime
- }
- // check example configuration
- if !foundFile {
- if example, modTime, err := getConfigurationFileWithENVPrefix(file, "example"); err == nil {
- if !watchMode && !configor.Silent {
- fmt.Printf("Failed to find configuration %v, using example file %v\n", file, example)
- }
- resultKeys = append(resultKeys, example)
- results[example] = modTime
- } else if !configor.Silent {
- fmt.Printf("Failed to find configuration %v\n", file)
- resultsErrors = append(resultsErrors, errors.New(fmt.Sprintf("Failed to find configuration %v\n", file)))
- }
- }
- }
- return resultKeys, results, resultsErrors
- }
- func processFile(config interface{}, file string, errorOnUnmatchedKeys bool) error {
- data, err := ioutil.ReadFile(file)
- if err != nil {
- return err
- }
- switch {
- case strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml"):
- if errorOnUnmatchedKeys {
- return yaml.UnmarshalStrict(data, config)
- }
- return yaml.Unmarshal(data, config)
- case strings.HasSuffix(file, ".toml"):
- return unmarshalToml(data, config, errorOnUnmatchedKeys)
- case strings.HasSuffix(file, ".json"):
- return unmarshalJSON(data, config, errorOnUnmatchedKeys)
- default:
- if err := unmarshalToml(data, config, errorOnUnmatchedKeys); err == nil {
- return nil
- } else if errUnmatchedKeys, ok := err.(*UnmatchedTomlKeysError); ok {
- return errUnmatchedKeys
- }
- if err := unmarshalJSON(data, config, errorOnUnmatchedKeys); err == nil {
- return nil
- } else if strings.Contains(err.Error(), "json: unknown field") {
- return err
- }
- var yamlError error
- if errorOnUnmatchedKeys {
- yamlError = yaml.UnmarshalStrict(data, config)
- } else {
- yamlError = yaml.Unmarshal(data, config)
- }
- if yamlError == nil {
- return nil
- } else if yErr, ok := yamlError.(*yaml.TypeError); ok {
- return yErr
- }
- return errors.New("failed to decode config")
- }
- }
- // GetStringTomlKeys returns a string array of the names of the keys that are passed in as args
- func GetStringTomlKeys(list []toml.Key) []string {
- arr := make([]string, len(list))
- for index, key := range list {
- arr[index] = key.String()
- }
- return arr
- }
- func unmarshalToml(data []byte, config interface{}, errorOnUnmatchedKeys bool) error {
- metadata, err := toml.Decode(string(data), config)
- if err == nil && len(metadata.Undecoded()) > 0 && errorOnUnmatchedKeys {
- return &UnmatchedTomlKeysError{Keys: metadata.Undecoded()}
- }
- return err
- }
- // unmarshalJSON unmarshals the given data into the config interface.
- // If the errorOnUnmatchedKeys boolean is true, an error will be returned if there
- // are keys in the data that do not match fields in the config interface.
- func unmarshalJSON(data []byte, config interface{}, errorOnUnmatchedKeys bool) error {
- reader := strings.NewReader(string(data))
- decoder := json.NewDecoder(reader)
- if errorOnUnmatchedKeys {
- decoder.DisallowUnknownFields()
- }
- err := decoder.Decode(config)
- if err != nil && err != io.EOF {
- return err
- }
- return nil
- }
- func getPrefixForStruct(prefixes []string, fieldStruct *reflect.StructField) []string {
- if fieldStruct.Anonymous && fieldStruct.Tag.Get("anonymous") == "true" {
- return prefixes
- }
- return append(prefixes, fieldStruct.Name)
- }
- func (configor *Configor) processDefaults(config interface{}) error {
- configValue := reflect.Indirect(reflect.ValueOf(config))
- if configValue.Kind() != reflect.Struct {
- return errors.New("invalid config, should be struct")
- }
- configType := configValue.Type()
- for i := 0; i < configType.NumField(); i++ {
- var (
- fieldStruct = configType.Field(i)
- field = configValue.Field(i)
- )
- if !field.CanAddr() || !field.CanInterface() {
- continue
- }
- if isBlank := reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface()); isBlank {
- // Set default configuration if blank
- if value := fieldStruct.Tag.Get("default"); value != "" {
- if err := yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
- return err
- }
- }
- }
- for field.Kind() == reflect.Ptr {
- field = field.Elem()
- }
- switch field.Kind() {
- case reflect.Struct:
- if err := configor.processDefaults(field.Addr().Interface()); err != nil {
- return err
- }
- case reflect.Slice:
- for i := 0; i < field.Len(); i++ {
- if reflect.Indirect(field.Index(i)).Kind() == reflect.Struct {
- if err := configor.processDefaults(field.Index(i).Addr().Interface()); err != nil {
- return err
- }
- }
- }
- }
- }
- return nil
- }
- func (configor *Configor) processTags(config interface{}, prefixes ...string) error {
- configValue := reflect.Indirect(reflect.ValueOf(config))
- if configValue.Kind() != reflect.Struct {
- return errors.New("invalid config, should be struct")
- }
- configType := configValue.Type()
- for i := 0; i < configType.NumField(); i++ {
- var (
- envNames []string
- fieldStruct = configType.Field(i)
- field = configValue.Field(i)
- envName = fieldStruct.Tag.Get("env") // read configuration from shell env
- )
- if !field.CanAddr() || !field.CanInterface() {
- continue
- }
- if envName == "" {
- envNames = append(envNames, strings.Join(append(prefixes, fieldStruct.Name), "_")) // Configor_DB_Name
- envNames = append(envNames, strings.ToUpper(strings.Join(append(prefixes, fieldStruct.Name), "_"))) // CONFIGOR_DB_NAME
- } else {
- envNames = []string{envName}
- }
- if configor.Config.Verbose {
- fmt.Printf("Trying to load struct `%v`'s field `%v` from env %v\n", configType.Name(), fieldStruct.Name, strings.Join(envNames, ", "))
- }
- // Load From Shell ENV
- for _, env := range envNames {
- if value := os.Getenv(env); value != "" {
- if configor.Config.Debug || configor.Config.Verbose {
- fmt.Printf("Loading configuration for struct `%v`'s field `%v` from env %v...\n", configType.Name(), fieldStruct.Name, env)
- }
- switch reflect.Indirect(field).Kind() {
- case reflect.Bool:
- switch strings.ToLower(value) {
- case "", "0", "f", "false":
- field.Set(reflect.ValueOf(false))
- default:
- field.Set(reflect.ValueOf(true))
- }
- case reflect.String:
- field.Set(reflect.ValueOf(value))
- default:
- if err := yaml.Unmarshal([]byte(value), field.Addr().Interface()); err != nil {
- return err
- }
- }
- break
- }
- }
- if isBlank := reflect.DeepEqual(field.Interface(), reflect.Zero(field.Type()).Interface()); isBlank && fieldStruct.Tag.Get("required") == "true" {
- // return error if it is required but blank
- return errors.New(fieldStruct.Name + " is required, but blank")
- }
- for field.Kind() == reflect.Ptr {
- field = field.Elem()
- }
- if field.Kind() == reflect.Struct {
- if err := configor.processTags(field.Addr().Interface(), getPrefixForStruct(prefixes, &fieldStruct)...); err != nil {
- return err
- }
- }
- if field.Kind() == reflect.Slice {
- if arrLen := field.Len(); arrLen > 0 {
- for i := 0; i < arrLen; i++ {
- if reflect.Indirect(field.Index(i)).Kind() == reflect.Struct {
- if err := configor.processTags(field.Index(i).Addr().Interface(), append(getPrefixForStruct(prefixes, &fieldStruct), fmt.Sprint(i))...); err != nil {
- return err
- }
- }
- }
- } else {
- // load slice from env
- newVal := reflect.New(field.Type().Elem()).Elem()
- if newVal.Kind() == reflect.Struct {
- idx := 0
- for {
- newVal = reflect.New(field.Type().Elem()).Elem()
- if err := configor.processTags(newVal.Addr().Interface(), append(getPrefixForStruct(prefixes, &fieldStruct), fmt.Sprint(idx))...); err != nil {
- return err
- } else if reflect.DeepEqual(newVal.Interface(), reflect.New(field.Type().Elem()).Elem().Interface()) {
- break
- } else {
- idx++
- field.Set(reflect.Append(field, newVal))
- }
- }
- }
- }
- }
- }
- return nil
- }
- func (configor *Configor) load(config interface{}, watchMode bool, files ...string) (err error, changed bool) {
- defer func() {
- if configor.Config.Debug || configor.Config.Verbose {
- if err != nil {
- fmt.Printf("Failed to load configuration from %v, got %v\n", files, err)
- }
- fmt.Printf("Configuration:\n %#v\n", config)
- }
- }()
- configFiles, configModTimeMap, resultsErrors := configor.getConfigurationFiles(watchMode, files...)
- if len(resultsErrors) > 0 {
- return resultsErrors[0], false
- }
- if watchMode {
- if len(configModTimeMap) == len(configor.configModTimes) {
- var changed bool
- for f, t := range configModTimeMap {
- if v, ok := configor.configModTimes[f]; !ok || t.After(v) {
- changed = true
- }
- }
- if !changed {
- return nil, false
- }
- }
- }
- // process defaults
- configor.processDefaults(config)
- for _, file := range configFiles {
- if configor.Config.Debug || configor.Config.Verbose {
- fmt.Printf("Loading configurations from file '%v'...\n", file)
- }
- if err = processFile(config, file, configor.GetErrorOnUnmatchedKeys()); err != nil {
- return err, true
- }
- }
- configor.configModTimes = configModTimeMap
- if prefix := configor.getENVPrefix(config); prefix == "-" {
- err = configor.processTags(config)
- } else {
- err = configor.processTags(config, prefix)
- }
- return err, true
- }
|