mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 14:20:54 +00:00
config ini support include
This commit is contained in:
parent
8ac2b9bf66
commit
14114018ea
16
config.go
16
config.go
@ -88,13 +88,13 @@ type beegoAppConfig struct {
|
|||||||
innerConfig config.ConfigContainer
|
innerConfig config.ConfigContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAppConfig(AppConfigProvider, AppConfigPath string) *beegoAppConfig {
|
func newAppConfig(AppConfigProvider, AppConfigPath string) (*beegoAppConfig, error) {
|
||||||
ac, err := config.NewConfig(AppConfigProvider, AppConfigPath)
|
ac, err := config.NewConfig(AppConfigProvider, AppConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ac = config.NewFakeConfig()
|
return nil, err
|
||||||
}
|
}
|
||||||
rac := &beegoAppConfig{ac}
|
rac := &beegoAppConfig{ac}
|
||||||
return rac
|
return rac, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *beegoAppConfig) Set(key, val string) error {
|
func (b *beegoAppConfig) Set(key, val string) error {
|
||||||
@ -281,15 +281,19 @@ func init() {
|
|||||||
err = ParseConfig()
|
err = ParseConfig()
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
// for init if doesn't have app.conf will not panic
|
// for init if doesn't have app.conf will not panic
|
||||||
Info(err)
|
ac := config.NewFakeConfig()
|
||||||
|
AppConfig = &beegoAppConfig{ac}
|
||||||
|
Warning(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseConfig parsed default config file.
|
// ParseConfig parsed default config file.
|
||||||
// now only support ini, next will support json.
|
// now only support ini, next will support json.
|
||||||
func ParseConfig() (err error) {
|
func ParseConfig() (err error) {
|
||||||
AppConfig = newAppConfig(AppConfigProvider, AppConfigPath)
|
AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
envRunMode := os.Getenv("BEEGO_RUNMODE")
|
envRunMode := os.Getenv("BEEGO_RUNMODE")
|
||||||
// set the runmode first
|
// set the runmode first
|
||||||
if envRunMode != "" {
|
if envRunMode != "" {
|
||||||
|
@ -48,6 +48,10 @@ type IniConfig struct {
|
|||||||
|
|
||||||
// ParseFile creates a new Config and parses the file configuration from the named file.
|
// ParseFile creates a new Config and parses the file configuration from the named file.
|
||||||
func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
|
func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
|
||||||
|
return ini.parseFile(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
|
||||||
file, err := os.Open(name)
|
file, err := os.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -66,6 +70,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
|
|||||||
|
|
||||||
var comment bytes.Buffer
|
var comment bytes.Buffer
|
||||||
buf := bufio.NewReader(file)
|
buf := bufio.NewReader(file)
|
||||||
|
// check the BOM
|
||||||
head, err := buf.Peek(3)
|
head, err := buf.Peek(3)
|
||||||
if err == nil && head[0] == 239 && head[1] == 187 && head[2] == 191 {
|
if err == nil && head[0] == 239 && head[1] == 187 && head[2] == 191 {
|
||||||
for i := 1; i <= 3; i++ {
|
for i := 1; i <= 3; i++ {
|
||||||
@ -114,13 +119,48 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
|
|||||||
cfg.data[section] = make(map[string]string)
|
cfg.data[section] = make(map[string]string)
|
||||||
}
|
}
|
||||||
keyValue := bytes.SplitN(line, bEqual, 2)
|
keyValue := bytes.SplitN(line, bEqual, 2)
|
||||||
|
|
||||||
|
key := string(bytes.TrimSpace(keyValue[0])) // key name case insensitive
|
||||||
|
key = strings.ToLower(key)
|
||||||
|
|
||||||
|
// handle include "other.conf"
|
||||||
|
if len(keyValue) == 1 && strings.HasPrefix(key, "include") {
|
||||||
|
includefiles := strings.Fields(key)
|
||||||
|
if includefiles[0] == "include" && len(includefiles) == 2 {
|
||||||
|
otherfile := strings.Trim(includefiles[1], "\"")
|
||||||
|
if !path.IsAbs(otherfile) {
|
||||||
|
otherfile = path.Join(path.Dir(name), otherfile)
|
||||||
|
}
|
||||||
|
i, err := ini.parseFile(otherfile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for sec, dt := range i.data {
|
||||||
|
if _, ok := cfg.data[sec]; !ok {
|
||||||
|
cfg.data[sec] = make(map[string]string)
|
||||||
|
}
|
||||||
|
for k, v := range dt {
|
||||||
|
cfg.data[sec][k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for sec, comm := range i.sectionComment {
|
||||||
|
cfg.sectionComment[sec] = comm
|
||||||
|
}
|
||||||
|
for k, comm := range i.keyComment {
|
||||||
|
cfg.keyComment[k] = comm
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(keyValue) != 2 {
|
||||||
|
return nil, errors.New("read the content error: \"" + string(line) + "\", should key = val")
|
||||||
|
}
|
||||||
val := bytes.TrimSpace(keyValue[1])
|
val := bytes.TrimSpace(keyValue[1])
|
||||||
if bytes.HasPrefix(val, bDQuote) {
|
if bytes.HasPrefix(val, bDQuote) {
|
||||||
val = bytes.Trim(val, `"`)
|
val = bytes.Trim(val, `"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
key := string(bytes.TrimSpace(keyValue[0])) // key name case insensitive
|
|
||||||
key = strings.ToLower(key)
|
|
||||||
cfg.data[section][key] = string(val)
|
cfg.data[section][key] = string(val)
|
||||||
if comment.Len() > 0 {
|
if comment.Len() > 0 {
|
||||||
cfg.keyComment[section+"."+key] = comment.String()
|
cfg.keyComment[section+"."+key] = comment.String()
|
||||||
|
Loading…
Reference in New Issue
Block a user