removed global

This commit is contained in:
Lars Hoogestraat 2018-03-27 22:11:26 +02:00
parent 6d945b2676
commit 9f2053babc
4 changed files with 25 additions and 12 deletions

12
cfg.go
View File

@ -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))

View File

@ -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)
}
}

View File

@ -1 +0,0 @@
package cfg

View File

@ -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