From 7f7e6b4ae4fb8835ef13ef329e7b9f260f8e891e Mon Sep 17 00:00:00 2001 From: miraclesu Date: Tue, 9 May 2017 18:40:19 +0800 Subject: [PATCH] =?UTF-8?q?conf:=20=E6=9B=BF=E6=8D=A2=20confutil=20?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/conf.go | 5 ++- utils/confutil.go | 93 ++++++++++++++++++++++++++++++++++++++++++ utils/confutil_test.go | 29 +++++++++++++ utils/test.json | 5 +++ utils/test1.json | 4 ++ 5 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 utils/confutil.go create mode 100644 utils/confutil_test.go create mode 100644 utils/test.json create mode 100644 utils/test1.json diff --git a/conf/conf.go b/conf/conf.go index 4e6c3b3..a59daa5 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -9,10 +9,11 @@ import ( "github.com/fsnotify/fsnotify" "github.com/go-gomail/gomail" - "sunteng/commons/confutil" "sunteng/commons/db/imgo" "sunteng/commons/event" "sunteng/commons/log" + + "github.com/shunfei/cronsun/utils" ) var ( @@ -113,7 +114,7 @@ func cleanKeyPrefix(p string) string { } func (c *Conf) parse() error { - err := confutil.LoadExtendConf(*confFile, c) + err := utils.LoadExtendConf(*confFile, c) if err != nil { return err } diff --git a/utils/confutil.go b/utils/confutil.go new file mode 100644 index 0000000..b6ab68c --- /dev/null +++ b/utils/confutil.go @@ -0,0 +1,93 @@ +// 加载json(可配置扩展字段)配置文件 +// +// { +// "Debug": true, +// "Log": "@extend:./log.json" +// } +package utils + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "regexp" +) + +var ( + extendTag = "@extend:" + pwdTag = "@pwd@" + rootTag = "@root@" + root = "" +) + +// 设置扩展标识,如果不设置,默认为 '@extend:' +func SetExtendTag(tag string) { + extendTag = tag +} + +func SetRoot(r string) { + root = r +} + +// 设置当前路径标识,如果不设置,默认为 '@pwd@' +// @pwd@ 会被替换成当前文件的路径, +// 至于是绝对路径还是相对路径,取决于读取文件时,传入的是绝对路径还是相对路径 +func SetPathTag(tag string) { + pwdTag = tag +} + +//加载json(可配置扩展字段)配置文件 +func LoadExtendConf(filePath string, v interface{}) error { + data, err := extendFile(filePath) + if err != nil { + return err + } + return json.Unmarshal(data, v) +} + +func extendFile(filePath string) (data []byte, err error) { + fi, err := os.Stat(filePath) + if err != nil { + return + } + if fi.IsDir() { + err = fmt.Errorf(filePath + " is not a file.") + return + } + + b, err := ioutil.ReadFile(filePath) + if err != nil { + return + } + + if len(root) != 0 { + b = bytes.Replace(b, []byte(rootTag), []byte(root), -1) + } + + dir := filepath.Dir(filePath) + return extendFileContent(dir, bytes.Replace(b, []byte(pwdTag), []byte(dir), -1)) +} + +func extendFileContent(dir string, content []byte) (data []byte, err error) { + //检查是不是规范的json + test := new(interface{}) + err = json.Unmarshal(content, &test) + if err != nil { + return + } + + // 替换子json文件 + reg := regexp.MustCompile(`"` + extendTag + `.*?"`) + data = reg.ReplaceAllFunc(content, func(match []byte) []byte { + match = match[len(extendTag)+1 : len(match)-1] + sb, e := extendFile(filepath.Join(dir, string(match))) + if e != nil { + err = fmt.Errorf("替换json配置[%s]失败:%s\n", match, e.Error()) + } + return sb + }) + return +} diff --git a/utils/confutil_test.go b/utils/confutil_test.go new file mode 100644 index 0000000..5d228f2 --- /dev/null +++ b/utils/confutil_test.go @@ -0,0 +1,29 @@ +package utils + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestLoadExtendConf(t *testing.T) { + testFile := "test.json" + + type conf struct { + Debug bool + Num int + Log struct { + Level int + Path string + } + } + Convey("confutil package test", t, func() { + Convey("load test file should be success", func() { + c := &conf{} + err := LoadExtendConf(testFile, c) + So(err, ShouldBeNil) + So(c.Debug, ShouldBeTrue) + So(c.Log.Path, ShouldEqual, "./tmp") + }) + }) +} diff --git a/utils/test.json b/utils/test.json new file mode 100644 index 0000000..df7c710 --- /dev/null +++ b/utils/test.json @@ -0,0 +1,5 @@ +{ + "Debug": true, + "Num": 1, + "Log": "@extend:test1.json" +} \ No newline at end of file diff --git a/utils/test1.json b/utils/test1.json new file mode 100644 index 0000000..fffdb15 --- /dev/null +++ b/utils/test1.json @@ -0,0 +1,4 @@ +{ + "Level": 2, + "Path": "@pwd@/tmp" +} \ No newline at end of file