summaryrefslogtreecommitdiff
path: root/plugins/config
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-02-16 22:08:50 +0300
committerValery Piashchynski <[email protected]>2021-02-16 22:08:50 +0300
commit8aa3d83f8c9471519a02f7779219238340fb86f8 (patch)
tree1d74b888a3793e3b3ac699913da45a603afe1625 /plugins/config
parentbb9dd8d3f46da089217e61efc3f058cfbba5ede3 (diff)
Add support for parsing env variables in the `.rr.yaml` config
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'plugins/config')
-rwxr-xr-xplugins/config/plugin.go24
1 files changed, 23 insertions, 1 deletions
diff --git a/plugins/config/plugin.go b/plugins/config/plugin.go
index ddb7fe88..a01a32b3 100755
--- a/plugins/config/plugin.go
+++ b/plugins/config/plugin.go
@@ -3,6 +3,7 @@ package config
import (
"bytes"
"fmt"
+ "os"
"strings"
"github.com/spf13/viper"
@@ -49,6 +50,12 @@ func (v *Viper) Init() error {
return errors.E(op, err)
}
+ // automatically inject ENV variables using ${ENV} pattern
+ for _, key := range v.viper.AllKeys() {
+ val := v.viper.Get(key)
+ v.viper.Set(key, parseEnv(val))
+ }
+
// override config Flags
if len(v.Flags) > 0 {
for _, f := range v.Flags {
@@ -61,7 +68,7 @@ func (v *Viper) Init() error {
}
}
- return v.viper.ReadInConfig()
+ return nil
}
// Overwrite overwrites existing config with provided values
@@ -125,3 +132,18 @@ func parseValue(value string) string {
return value
}
+
+func parseEnv(value interface{}) interface{} {
+ str, ok := value.(string)
+ if !ok || len(str) <= 3 {
+ return value
+ }
+
+ if str[0:2] == "${" && str[len(str)-1:] == "}" {
+ if v, ok := os.LookupEnv(str[2 : len(str)-1]); ok {
+ return v
+ }
+ }
+
+ return str
+}