summaryrefslogtreecommitdiffstats
path: root/bridge/config/config.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-10-30 22:32:29 +0100
committerWim <wim@42.be>2016-10-30 22:32:29 +0100
commit92d9db5a2d6f568e3c21ecf56cde80b32c342868 (patch)
tree00ce7d6ce9e350ebc162bb6b85ba123a9b443a70 /bridge/config/config.go
parent96620a3c2c3ab70d0e451a19dccd20ee76416937 (diff)
downloadmatterbridge-msglm-92d9db5a2d6f568e3c21ecf56cde80b32c342868.tar.gz
matterbridge-msglm-92d9db5a2d6f568e3c21ecf56cde80b32c342868.tar.bz2
matterbridge-msglm-92d9db5a2d6f568e3c21ecf56cde80b32c342868.zip
Override config from environment. See #50
Expects uppercase environment variables of MATTERBRIDGE_PROTOCOL_ACCOUNT_KEY="value" e.g. you can override this config [mattermost] [mattermost.work] Team="yourteam" Login="yourlogin" Password="yourpass" by using MATTERBRIDGE_MATTERMOST_WORK_TEAM="newteam" MATTERBRIDGE_MATTERMOST_WORK_LOGIN="newlogin" MATTERBRIDGE_MATTERMOST_WORK_PASSWORD="newpassword"
Diffstat (limited to 'bridge/config/config.go')
-rw-r--r--bridge/config/config.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/bridge/config/config.go b/bridge/config/config.go
index 3972e84e..aa881a79 100644
--- a/bridge/config/config.go
+++ b/bridge/config/config.go
@@ -3,6 +3,9 @@ package config
import (
"github.com/BurntSushi/toml"
"log"
+ "os"
+ "reflect"
+ "strings"
)
type Message struct {
@@ -80,3 +83,40 @@ func NewConfig(cfgfile string) *Config {
}
return &cfg
}
+
+func OverrideCfgFromEnv(cfg *Config, protocol string, account string) {
+ var protoCfg Protocol
+ val := reflect.ValueOf(cfg).Elem()
+ // loop over the Config struct
+ for i := 0; i < val.NumField(); i++ {
+ typeField := val.Type().Field(i)
+ // look for the protocol map (both lowercase)
+ if strings.ToLower(typeField.Name) == protocol {
+ // get the Protocol struct from the map
+ data := val.Field(i).MapIndex(reflect.ValueOf(account))
+ protoCfg = data.Interface().(Protocol)
+ protoStruct := reflect.ValueOf(&protoCfg).Elem()
+ // loop over the found protocol struct
+ for i := 0; i < protoStruct.NumField(); i++ {
+ typeField := protoStruct.Type().Field(i)
+ // build our environment key (eg MATTERBRIDGE_MATTERMOST_WORK_LOGIN)
+ key := "matterbridge_" + protocol + "_" + account + "_" + typeField.Name
+ key = strings.ToUpper(key)
+ // search the environment
+ res := os.Getenv(key)
+ // if it exists and the current field is a string
+ // then update the current field
+ if res != "" {
+ fieldVal := protoStruct.Field(i)
+ if fieldVal.Kind() == reflect.String {
+ log.Printf("config: overriding %s from env with %s\n", key, res)
+ fieldVal.Set(reflect.ValueOf(res))
+ }
+ }
+ }
+ // update the map with the modified Protocol (cfg.Protocol[account] = Protocol)
+ val.Field(i).SetMapIndex(reflect.ValueOf(account), reflect.ValueOf(protoCfg))
+ break
+ }
+ }
+}