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