-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#38]: feat: Allow to include nested configurations
- Loading branch information
Showing
10 changed files
with
761 additions
and
605 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package config | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/roadrunner-server/errors" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func getConfiguration(path, prefix string) (map[string]any, string, error) { | ||
v := viper.New() | ||
v.AutomaticEnv() | ||
v.SetEnvPrefix(prefix) | ||
v.SetConfigFile(path) | ||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
err := v.ReadInConfig() | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
// get configuration version | ||
ver := v.Get(versionKey) | ||
if ver == nil { | ||
return nil, "", errors.Str("rr configuration file should contain a version e.g: version: 2.7") | ||
} | ||
|
||
if _, ok := ver.(string); !ok { | ||
return nil, "", errors.Errorf("type of version should be string, actual: %T", ver) | ||
} | ||
|
||
// automatically inject ENV variables using ${ENV} pattern | ||
expandEnvViper(v) | ||
|
||
return v.AllSettings(), ver.(string), nil | ||
} | ||
|
||
func (p *Plugin) handleInclude(rootVersion string) error { | ||
ifiles := p.viper.Get(includeKey) | ||
if ifiles != nil { | ||
if _, ok := ifiles.([]string); !ok { | ||
return errors.Str("include should be an array of strings") | ||
} | ||
|
||
includeFiles := ifiles.([]string) | ||
for _, file := range includeFiles { | ||
dir, _ := filepath.Split(p.Path) | ||
config, version, err := getConfiguration(filepath.Join(dir, file), p.Prefix) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if version != rootVersion { | ||
return errors.Str("version in included file must be the same as in root") | ||
} | ||
|
||
// overriding configuration | ||
for key, val := range config { | ||
p.viper.Set(key, val) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.