1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-12 22:20:38 +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

@ -121,10 +121,12 @@ type ConfigContainer struct {
// Bool returns the boolean value for a given key.
func (c *ConfigContainer) Bool(key string) (bool, error) {
if v, ok := c.data[key]; ok {
if v, err := c.getData(key); err != nil {
return false, err
} else {
return config.ParseBool(v)
}
return false, fmt.Errorf("not exist key: %q", key)
}
// DefaultBool return the bool value if has no error
@ -139,8 +141,12 @@ func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
// Int returns the integer value for a given key.
func (c *ConfigContainer) Int(key string) (int, error) {
if v, ok := c.data[key].(int64); ok {
return int(v), nil
if v, err := c.getData(key); err != nil {
return 0, err
} else if vv, ok := v.(int); ok {
return vv, nil
} else if vv, ok := v.(int64); ok {
return int(vv), nil
}
return 0, errors.New("not int value")
}
@ -157,8 +163,10 @@ func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
// Int64 returns the int64 value for a given key.
func (c *ConfigContainer) Int64(key string) (int64, error) {
if v, ok := c.data[key].(int64); ok {
return v, nil
if v, err := c.getData(key); err != nil {
return 0, err
} else if vv, ok := v.(int64); ok {
return vv, nil
}
return 0, errors.New("not bool value")
}
@ -175,8 +183,14 @@ func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
// Float returns the float value for a given key.
func (c *ConfigContainer) Float(key string) (float64, error) {
if v, ok := c.data[key].(float64); ok {
return v, nil
if v, err := c.getData(key); err != nil {
return 0.0, err
} else if vv, ok := v.(float64); ok {
return vv, nil
} else if vv, ok := v.(int); ok {
return float64(vv), nil
} else if vv, ok := v.(int64); ok {
return float64(vv), nil
}
return 0.0, errors.New("not float64 value")
}
@ -193,8 +207,10 @@ func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
// String returns the string value for a given key.
func (c *ConfigContainer) String(key string) string {
if v, ok := c.data[key].(string); ok {
return v
if v, err := c.getData(key); err == nil {
if vv, ok := v.(string); ok {
return vv
}
}
return ""
}
@ -228,7 +244,27 @@ func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []stri
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
v, ok := c.data[section]
if ok {
return v.(map[string]string), nil
var interfaceToStr func(map[string]interface{}) map[string]string
interfaceToStr = func(values map[string]interface{}) map[string]string {
strValues := make(map[string]string, len(values))
for k, vv := range values {
if vv == nil {
strValues[k] = ""
} else if env, ok := config.Getenv(vv); ok {
strValues[k] = env
} else if str, ok := vv.(string); ok {
strValues[k] = str
} else if m, ok := vv.(map[string]interface{}); ok {
strValues[k] = fmt.Sprintf("%v", interfaceToStr(m))
} else {
// TODO: no better.
strValues[k] = fmt.Sprintf("%v", vv)
}
}
return strValues
}
return interfaceToStr(v.(map[string]interface{})), nil
}
return nil, errors.New("not exist setction")
}
@ -255,10 +291,23 @@ func (c *ConfigContainer) Set(key, val string) error {
// DIY returns the raw value by a given key.
func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
if v, ok := c.data[key]; ok {
return v, nil
return c.getData(key)
}
func (c *ConfigContainer) getData(key string) (interface{}, error) {
if len(key) == 0 {
return nil, errors.New("key is emtpy")
}
return nil, errors.New("not exist key")
if v, ok := c.data[key]; ok {
if env, ok := config.Getenv(v); ok {
return env, nil
} else {
return v, nil
}
}
return nil, fmt.Errorf("not exist key %q", key)
}
func init() {

View File

@ -16,12 +16,16 @@ package yaml
import (
"os"
"strings"
"testing"
"github.com/astaxie/beego/config"
)
var yamlcontext = `
func TestYaml(t *testing.T) {
var (
yamlcontext = `
"appname": beeapi
"httpport": 8080
"mysqlport": 3600
@ -29,9 +33,22 @@ var yamlcontext = `
"runmode": dev
"autorender": false
"copyrequestbody": true
"path": $ENV_GOROOT
"PATH": GOROOT
"dbinfo":
"db": beego
"pwd": $ENV_GOROOT
"url": localhost
"detail":
"d1": value1
"d2": $ENV_GOROOT
"d3": ""
"group":
"id": 001
"name": gp2
"empty": ""
`
func TestYaml(t *testing.T) {
)
f, err := os.Create("testyaml.conf")
if err != nil {
t.Fatal(err)
@ -47,6 +64,7 @@ func TestYaml(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if yamlconf.String("appname") != "beeapi" {
t.Fatal("appname not equal to beeapi")
}
@ -79,4 +97,12 @@ func TestYaml(t *testing.T) {
if yamlconf.String("name") != "astaxie" {
t.Fatal("get name error")
}
if dbinfo, err := yamlconf.GetSection("dbinfo"); err != nil {
t.Fatal(err)
} else if dbinfo["pwd"] != os.Getenv("GOROOT") {
t.Fatal("get pwd error")
} else if strings.Contains(dbinfo["detail"], os.Getenv("GOROOT")) == false {
t.Fatal("get GOROOT path error")
}
}