mirror of
https://github.com/astaxie/beego.git
synced 2025-07-06 16:10: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:
@ -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,
|
||||
|
Reference in New Issue
Block a user