|
| 1 | +package rpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/roadrunner-server/errors" |
| 5 | + "github.com/spf13/viper" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + versionKey string = "version" |
| 10 | + includeKey string = "include" |
| 11 | + defaultConfigVersion string = "3" |
| 12 | + prevConfigVersion string = "2.7" |
| 13 | +) |
| 14 | + |
| 15 | +func getConfiguration(path string) (map[string]any, string, error) { |
| 16 | + v := viper.New() |
| 17 | + v.SetConfigFile(path) |
| 18 | + err := v.ReadInConfig() |
| 19 | + if err != nil { |
| 20 | + return nil, "", err |
| 21 | + } |
| 22 | + |
| 23 | + // get configuration version |
| 24 | + ver := v.Get(versionKey) |
| 25 | + if ver == nil { |
| 26 | + return nil, "", errors.Str("rr configuration file should contain a version e.g: version: 2.7") |
| 27 | + } |
| 28 | + |
| 29 | + if _, ok := ver.(string); !ok { |
| 30 | + return nil, "", errors.Errorf("type of version should be string, actual: %T", ver) |
| 31 | + } |
| 32 | + |
| 33 | + // automatically inject ENV variables using ${ENV} pattern |
| 34 | + expandEnvViper(v) |
| 35 | + |
| 36 | + return v.AllSettings(), ver.(string), nil |
| 37 | +} |
| 38 | + |
| 39 | +func handleInclude(rootVersion string, v *viper.Viper) error { |
| 40 | + // automatically inject ENV variables using ${ENV} pattern |
| 41 | + // root config |
| 42 | + expandEnvViper(v) |
| 43 | + |
| 44 | + ifiles := v.GetStringSlice(includeKey) |
| 45 | + if ifiles == nil { |
| 46 | + return nil |
| 47 | + } |
| 48 | + |
| 49 | + for _, file := range ifiles { |
| 50 | + config, version, err := getConfiguration(file) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + if version != rootVersion { |
| 56 | + return errors.Str("version in included file must be the same as in root") |
| 57 | + } |
| 58 | + |
| 59 | + // overriding configuration |
| 60 | + for key, val := range config { |
| 61 | + v.Set(key, val) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
0 commit comments