2017-03-10 09:08:51 +00:00
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-04-02 02:19:52 +00:00
|
|
|
"gopkg.in/ini.v1"
|
2017-03-10 09:08:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 读取配置
|
2017-03-14 06:31:46 +00:00
|
|
|
func Read(filename string) (config *ini.File, err error) {
|
|
|
|
config, err = ini.Load(filename)
|
2017-03-10 09:08:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 写入配置
|
2017-04-02 02:19:52 +00:00
|
|
|
func Write(config map[string]map[string]string, filename string) error {
|
2017-03-10 09:08:51 +00:00
|
|
|
if len(config) == 0 {
|
|
|
|
return errors.New("参数不能为空")
|
|
|
|
}
|
|
|
|
|
|
|
|
file := ini.Empty()
|
2017-04-02 02:19:52 +00:00
|
|
|
for sectionName, items := range config {
|
2017-03-10 09:08:51 +00:00
|
|
|
if sectionName == "" {
|
|
|
|
return errors.New("节名称不能为空")
|
|
|
|
}
|
|
|
|
section, err := file.NewSection(sectionName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-02 02:19:52 +00:00
|
|
|
for key, value := range items {
|
2017-03-10 09:08:51 +00:00
|
|
|
_, err = section.NewKey(key, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 06:31:46 +00:00
|
|
|
err := file.SaveTo(filename)
|
2017-03-10 09:08:51 +00:00
|
|
|
|
|
|
|
return err
|
2017-04-02 02:19:52 +00:00
|
|
|
}
|