-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
70 lines (63 loc) · 1.38 KB
/
conf.go
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
package gconf
import (
"fmt"
"io/ioutil"
"path"
"strings"
"github.com/BurntSushi/toml"
"github.com/usthooz/gconf/json"
"github.com/usthooz/gconf/xml"
"github.com/usthooz/gconf/yaml"
)
const (
JsonSub string = "json"
YamlSub string = "yaml"
TomlSub string = "toml"
XmlSub string = "xml"
)
const (
// default config path
DefaultConfPath = "./config/config.yaml"
)
type Gconf struct {
// ConfPath config file path->default: ./config/config.yaml
ConfPath string
// Subffix config file subffix
Subffix string
}
// NewConf new conf object
func NewConf(confParam *Gconf) *Gconf {
if len(confParam.ConfPath) == 0 {
confParam.ConfPath = DefaultConfPath
}
return confParam
}
/*
confpath: config file path->default: ./config/config.yaml
subffix: config file subffie->option
*/
func (oozConf *Gconf) GetConf(conf interface{}) error {
// read config file
bs, err := ioutil.ReadFile(oozConf.ConfPath)
if err != nil {
return err
}
if len(oozConf.Subffix) == 0 {
// get file subffix
oozConf.Subffix = strings.Trim(path.Ext(path.Base(oozConf.ConfPath)), ".")
}
// check analy
switch oozConf.Subffix {
case JsonSub:
err = json.Unmarshal(bs, conf)
case TomlSub:
err = toml.Unmarshal(bs, conf)
case YamlSub:
err = yaml.Unmarshal(bs, conf)
case XmlSub:
err = xml.Unmarshal(bs, conf)
default:
err = fmt.Errorf("GetConf: non support this file type...")
}
return err
}