removed global
This commit is contained in:
parent
6d945b2676
commit
9f2053babc
12
cfg.go
12
cfg.go
|
@ -13,12 +13,6 @@ import (
|
|||
"unicode"
|
||||
)
|
||||
|
||||
var cfg *Config
|
||||
|
||||
func init() {
|
||||
cfg = new(Config)
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Files []File
|
||||
}
|
||||
|
@ -28,16 +22,16 @@ type File struct {
|
|||
Path string
|
||||
}
|
||||
|
||||
func AddConfig(path, name string) {
|
||||
func (c *Config) AddConfig(path, name string) {
|
||||
f := File{
|
||||
Path: path,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
cfg.Files = append(cfg.Files, f)
|
||||
c.Files = append(c.Files, f)
|
||||
}
|
||||
|
||||
func (c Config) MergeConfigsInto(dest interface{}) error {
|
||||
func (c *Config) MergeConfigsInto(dest interface{}) error {
|
||||
for _, v := range c.Files {
|
||||
f, err := os.Open(filepath.Join(v.Path, v.Name))
|
||||
|
||||
|
|
20
cfg_test.go
20
cfg_test.go
|
@ -65,7 +65,7 @@ func TestStandardConfig(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestInnerStruct(t *testing.T) {
|
||||
type Config struct {
|
||||
type config struct {
|
||||
Server struct {
|
||||
Address string `cfg:"server_address"`
|
||||
Port int `cfg:"server_port"`
|
||||
|
@ -74,7 +74,7 @@ func TestInnerStruct(t *testing.T) {
|
|||
|
||||
AddConfig("./testcfg", "config.conf")
|
||||
|
||||
c := new(Config)
|
||||
c := new(config)
|
||||
err := cfg.MergeConfigsInto(c)
|
||||
|
||||
if err != nil {
|
||||
|
@ -88,3 +88,19 @@ func TestInnerStruct(t *testing.T) {
|
|||
t.Errorf("server.Port expected to be 42 but was %d", c.Server.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArrayConfig(t *testing.T) {
|
||||
type config struct {
|
||||
GroupList []string `cfg:"group_list"`
|
||||
}
|
||||
|
||||
AddConfig("./testcfg", "config.conf")
|
||||
|
||||
c := new(config)
|
||||
|
||||
err := cfg.MergeConfigsInto(c)
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,5 +4,9 @@ port = 8080
|
|||
ssl = true
|
||||
session_timeout = 10m
|
||||
|
||||
#A comment
|
||||
server_address = localhost
|
||||
server_port = 42
|
||||
|
||||
#Array
|
||||
group_list = Admin,User,Moderator
|
||||
|
|
Loading…
Reference in New Issue