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

@ -92,7 +92,7 @@ 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 := c.getData(key); v != nil {
return config.ParseBool(v)
}
return false, fmt.Errorf("not exist key: %q", key)
@ -110,7 +110,7 @@ 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) {
return strconv.Atoi(c.data[key].(string))
return strconv.Atoi(c.getData(key).(string))
}
// DefaultInt returns the integer value for a given key.
@ -125,7 +125,7 @@ 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) {
return strconv.ParseInt(c.data[key].(string), 10, 64)
return strconv.ParseInt(c.getData(key).(string), 10, 64)
}
// DefaultInt64 returns the int64 value for a given key.
@ -141,7 +141,7 @@ 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) {
return strconv.ParseFloat(c.data[key].(string), 64)
return strconv.ParseFloat(c.getData(key).(string), 64)
}
// DefaultFloat returns the float64 value for a given key.
@ -156,7 +156,7 @@ 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 {
if v, ok := c.getData(key).(string); ok {
return v
}
return ""
@ -190,7 +190,28 @@ func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []stri
// GetSection returns map for the given section
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; 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")
}
@ -227,6 +248,18 @@ func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
return nil, errors.New("not exist key")
}
// Get Data
func (c *ConfigContainer) getData(key string) interface{} {
if v, ok := c.data[key]; ok {
if env, ok := config.Getenv(v); ok {
return env
}
return v
}
return nil
}
func init() {
config.Register("xml", &Config{})
}

View File

@ -16,13 +16,16 @@ package xml
import (
"os"
"strings"
"testing"
"github.com/astaxie/beego/config"
)
//xml parse should incluce in <config></config> tags
var xmlcontext = `<?xml version="1.0" encoding="UTF-8"?>
func TestXML(t *testing.T) {
//xml parse should incluce in <config></config> tags
var xmlcontext = `<?xml version="1.0" encoding="UTF-8"?>
<config>
<appname>beeapi</appname>
<httpport>8080</httpport>
@ -31,10 +34,24 @@ var xmlcontext = `<?xml version="1.0" encoding="UTF-8"?>
<runmode>dev</runmode>
<autorender>false</autorender>
<copyrequestbody>true</copyrequestbody>
<path>$ENV_GOROOT</path>
<dbinfo>
<db>beego</db>
<pwd>$ENV_GOROOT</pwd>
<url>localhost</url>
<detail>
<d1>value1</d1>
<d2>$ENV_GOROOT</d2>
<d3></d3>
</detail>
<group>
<id>001</id>
<name>gp2</name>
</group>
</dbinfo>
</config>
`
func TestXML(t *testing.T) {
f, err := os.Create("testxml.conf")
if err != nil {
t.Fatal(err)
@ -82,4 +99,16 @@ func TestXML(t *testing.T) {
if xmlconf.String("name") != "astaxie" {
t.Fatal("get name error")
}
if xmlconf.String("path") == os.Getenv("GOROOT") {
t.Fatal("get path error")
}
if dbinfo, err := xmlconf.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")
}
}