github.com/TIBCOSoftware/flogo-lib@v0.5.9/app/propertyresolver/env.go (about) 1 package propertyresolver 2 3 import ( 4 "encoding/json" 5 "os" 6 "strings" 7 8 "github.com/TIBCOSoftware/flogo-lib/app" 9 "github.com/TIBCOSoftware/flogo-lib/logger" 10 ) 11 12 var logEnv = logger.GetLogger("app-props-env-resolver") 13 14 const EnvAppPropertyEnvConfigKey = "FLOGO_APP_PROPS_ENV" 15 16 type PropertyMappings struct { 17 Mappings map[string]string `json:"mappings"` 18 } 19 20 var mapping PropertyMappings 21 22 func init() { 23 app.RegisterPropertyValueResolver("env", &EnvVariableValueResolver{}) 24 mappings := getEnvValue() 25 if mappings != "" { 26 e := json.Unmarshal([]byte(mappings), &mapping) 27 if e != nil { 28 logEnv.Errorf("Can not parse value set to '%s' due to error - '%v'", EnvAppPropertyEnvConfigKey, e) 29 panic("") 30 } 31 } 32 } 33 34 func getEnvValue() string { 35 key := os.Getenv(EnvAppPropertyEnvConfigKey) 36 if len(key) > 0 { 37 return key 38 } 39 return "" 40 } 41 42 // Resolve property value from environment variable 43 type EnvVariableValueResolver struct { 44 } 45 46 func (resolver *EnvVariableValueResolver) LookupValue(key string) (interface{}, bool) { 47 value, exists := os.LookupEnv(key) // first try with the name of the property as is 48 if exists { 49 return value, exists 50 } 51 52 // Lookup based on mapping defined 53 keyMapping, ok := mapping.Mappings[key] 54 if ok { 55 return os.LookupEnv(keyMapping) 56 } 57 58 // Replace dot with underscore e.g. a.b would be a_b 59 key = strings.Replace(key, ".", "_", -1) 60 value, exists = os.LookupEnv(key) 61 if exists { 62 return value, exists 63 } 64 65 66 // Try upper case form e.g. a.b would be A_B 67 key = strings.ToUpper(key) 68 value, exists = os.LookupEnv(key) // if not found try with the canonical form 69 return value, exists 70 }