1
0
mirror of https://github.com/astaxie/beego.git synced 2025-07-06 14:00:19 +00:00

Config support get environment variable

get environment variable if config item  has prefix "$ENV_" .
e.g.
```ini
[demo]
password = $ENV_MyPWD
```
This commit is contained in:
ysqi
2016-01-27 20:46:30 +08:00
parent 4ce094a29a
commit cd31c816cc
10 changed files with 222 additions and 32 deletions

View File

@ -44,6 +44,8 @@ package config
import (
"fmt"
"os"
"strings"
)
// Configer defines how to get and set value from configuration raw data.
@ -107,6 +109,24 @@ func NewConfigData(adapterName string, data []byte) (Configer, error) {
return adapter.ParseData(data)
}
const envKeySign = "$ENV_"
// Getenv return environment variable if env has prefix "$ENV_".
func Getenv(env interface{}) (string, bool) {
if env == nil {
return "", false
}
// Onley support string key.
if key, ok := env.(string); ok {
if len(key) > len(envKeySign) && strings.HasPrefix(key, envKeySign) {
key = strings.TrimLeft(key, envKeySign)
return os.Getenv(key), true
}
}
return "", false
}
// ParseBool returns the boolean value represented by the string.
//
// It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on, On,