Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions argv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var _ = Describe("ArgvConfig", func() {
BeforeEach(func() {
cfg = NewArgvConfig("test")
err = cfg.Load()
Expect(err == nil)
})
It("Should load variables from commandline", func() {
Expect(len(cfg.All()) >= 0).To(BeTrue())
Expand Down
4 changes: 2 additions & 2 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func NewEnvConfig(prefix string) ReadableConfig {
func (self *EnvConfig) Load() (err error) {
env := os.Environ()
for _, pair := range env {
kv := strings.Split(pair, "=")
if kv != nil && len(kv) >= 2 {
kv := strings.SplitN(pair, "=", 2)
if kv != nil && len(kv) == 2 {
self.Set(strings.Replace(kv[0], self.Prefix, "", 1), kv[1])
}
}
Expand Down
8 changes: 4 additions & 4 deletions env_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gonfig_test
package gonfig

import (
. "github.com/Nomon/gonfig"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"os"
Expand All @@ -16,14 +15,15 @@ var _ = Describe("EnvConfig", func() {
BeforeEach(func() {
cfg = NewEnvConfig("")
err = cfg.Load()
Expect(err == nil)
})
It("Should load variables from environment", func() {
Expect(len(cfg.All()) > 0).To(BeTrue())
env := os.Environ()
Expect(len(env) > 0).To(BeTrue())
for _, kvpair := range env {
pairs := strings.Split(kvpair, "=")
Expect(len(pairs) >= 2).To(BeTrue())
pairs := strings.SplitN(kvpair, "=", 2)
Expect(len(pairs) == 2).To(BeTrue())
Expect(cfg.Get(pairs[0])).To(Equal(pairs[1]))
}
})
Expand Down
17 changes: 14 additions & 3 deletions gonfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type Gonfig struct {
Configurable
// named configurables, these are iterated if key is not found in Config
Configs map[string]Configurable

keys []string
// Defaults configurable, if key is not found in the Configurable & Configurables in Config,
//Defaults is checked for fallback values
Defaults Configurable
Expand All @@ -74,6 +76,7 @@ func NewConfig(initial Configurable, defaults ...Configurable) *Gonfig {
return &Gonfig{
initial,
make(map[string]Configurable),
make([]string, 0, 1),
defaults[0],
}
}
Expand Down Expand Up @@ -116,7 +119,8 @@ func (self *Gonfig) Reset(datas ...map[string]string) {
if len(datas) > 0 {
data = datas[0]
}
for _, value := range self.Configs {
for _, name := range self.keys {
value := self.Configs[name]
if data != nil {
value.Reset(data)
} else {
Expand All @@ -139,11 +143,13 @@ func (self *Gonfig) Reset(datas ...map[string]string) {
func (self *Gonfig) Use(name string, config ...Configurable) Configurable {
if self.Configs == nil {
self.Configs = make(map[string]Configurable)
self.keys = make([]string, 0, len(config))
}
if len(config) == 0 {
return self.Configs[name]
}
self.Configs[name] = config[0]
self.keys = append(self.keys, name)
LoadConfig(self.Configs[name])
return self.Configs[name]
}
Expand All @@ -155,7 +161,11 @@ func (self *Gonfig) Get(key string) string {
return value
}
// go through all in insert order untill key is found
for _, config := range self.Configs {
for _, name := range self.keys {
config := self.Configs[name]
if config == nil {
break
}
if value := config.Get(key); value != "" {
return value
}
Expand All @@ -182,7 +192,8 @@ func SaveConfig(config Configurable) error {

// Saves all mounted configurations in the hierarchy that implement the WritableConfig interface
func (self *Gonfig) Save() error {
for _, config := range self.Configs {
for _, name := range self.keys {
config := self.Configs[name]
if err := SaveConfig(config); err != nil {
return err
}
Expand Down