conf.go 1.2 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
// 读取配置信息
package conf

import (
	"io/ioutil"
	"os"

	"gopkg.in/yaml.v2"
)

var (
	_Configure map[string]string
	lastFile   = ""
)

func Get(name string) (string, bool) {
	v, has := _Configure[name]
	return v, has
}
func Set(name, value string) {
	_Configure[name] = value
}

func Value(name string) string {
	return _Configure[name]
}
func Reload() {
	ReadConfigure(lastFile)
}
func MastValue(name string, def string) string {
	v, h := _Configure[name]
	if h {
		return v
	}
	return def
}
func ReadConfigure(filepath string) error {
	file, err := os.Open(filepath)
	if err != nil {
		return err
	}
	content, err := ioutil.ReadAll(file)
	if err != nil {
		return err
	}
	configure := make(map[string]string)
	err = yaml.Unmarshal(content, &configure)
	if err != nil {
		return err
	}
	_Configure = configure
	lastFile = filepath
	return nil
}

// 更新配置文件
func Save() (bool, error) {
	//file, err := os.OpenFile(lastFile, os.O_CREATE|os.O_WRONLY, 0666)
	//if err != nil {
	//	panic(err)
	//}
	//defer file.Close()

	confStr, err := yaml.Marshal(_Configure)
	if err != nil {
		return false, err
	}

	ioutil.WriteFile(lastFile, confStr, 0666)
	return true, nil
}