1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 15:11:29 +00:00

Merge pull request #1158 from mlgd/master

Fix save config ini file
This commit is contained in:
astaxie 2015-05-18 22:38:59 +08:00
commit 59c1e74e13

View File

@ -300,7 +300,32 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
defer f.Close() defer f.Close()
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
// Save default section at first place
if dt, ok := c.data[DEFAULT_SECTION]; ok {
for key, val := range dt {
if key != " " {
// Write key comments.
if v, ok := c.keyComment[key]; ok {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil {
return err
}
}
// Write key and value.
if _, err = buf.WriteString(key + string(bEqual) + val + lineBreak); err != nil {
return err
}
}
}
// Put a line between sections.
if _, err = buf.WriteString(lineBreak); err != nil {
return err
}
}
// Save named sections
for section, dt := range c.data { for section, dt := range c.data {
if section != DEFAULT_SECTION {
// Write section comments. // Write section comments.
if v, ok := c.sectionComment[section]; ok { if v, ok := c.sectionComment[section]; ok {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil { if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil {
@ -308,12 +333,10 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
} }
} }
if section != DEFAULT_SECTION {
// Write section name. // Write section name.
if _, err = buf.WriteString(string(sectionStart) + section + string(sectionEnd) + lineBreak); err != nil { if _, err = buf.WriteString(string(sectionStart) + section + string(sectionEnd) + lineBreak); err != nil {
return err return err
} }
}
for key, val := range dt { for key, val := range dt {
if key != " " { if key != " " {
@ -336,6 +359,7 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
return err return err
} }
} }
}
if _, err = buf.WriteTo(f); err != nil { if _, err = buf.WriteTo(f); err != nil {
return err return err