From 43f87b9b57e29433fd5f3ee394c615fc33630abb Mon Sep 17 00:00:00 2001 From: Cody Soyland Date: Tue, 17 Dec 2013 15:32:17 -0600 Subject: [PATCH] More config changes. --- config/config.go | 46 ++++++++++++++++++++++++++++--------------- config/config_test.go | 30 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/config/config.go b/config/config.go index 882489e50..9cd3419e3 100644 --- a/config/config.go +++ b/config/config.go @@ -11,23 +11,16 @@ import ( ) type Config struct { - config map[string]interface{} - lock sync.RWMutex - loaded bool + config map[string]interface{} + lock sync.RWMutex + filename string + loaded bool } var config *Config func init() { - config_file := os.Getenv("PILOSA_CONFIG") - if config_file == "" { - config_file = "pilosa.yaml" - } - var err error - config, err = NewConfig(config_file) - if err != nil { - log.Fatal(err) - } + config = NewConfig("") } func GetSafe(key string) (interface{}, bool) { @@ -46,18 +39,38 @@ func GetString(key string) string { return config.GetString(key) } -func NewConfig(filename string) (*Config, error) { +func NewConfig(filename string) *Config { self := Config{} self.config = make(map[string]interface{}) - data, err := ioutil.ReadFile(filename) + self.filename = filename + return &self +} + +func (self *Config) load() error { + self.lock.Lock() + defer self.lock.Unlock() + if self.loaded { + return nil + } + config_file := self.filename + if config_file == "" { + config_file = os.Getenv("PILOSA_CONFIG") + if config_file == "" { + log.Println("PILOSA_CONFIG not set, defaulting to pilosa.yaml") + config_file = "pilosa.yaml" + } + } + data, err := ioutil.ReadFile(config_file) if err != nil { - return nil, errors.New("Problem with config file: " + err.Error()) + return errors.New("Problem with config file: " + err.Error()) } goyaml.Unmarshal(data, self.config) - return &self, nil + self.loaded = true + return nil } func (self *Config) GetSafe(key string) (interface{}, bool) { + self.load() self.lock.RLock() defer self.lock.RUnlock() value, ok := self.config[key] @@ -65,6 +78,7 @@ func (self *Config) GetSafe(key string) (interface{}, bool) { } func (self *Config) Get(key string) interface{} { + self.load() self.lock.RLock() defer self.lock.RUnlock() return self.config[key] diff --git a/config/config_test.go b/config/config_test.go index af25d2c82..967638791 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -34,3 +34,33 @@ func TestConfig(t *testing.T) { So(GetString("temp"), ShouldEqual, "/tmp") }) } + +func TestConfigObject(t *testing.T) { + err := os.Setenv("PILOSA_CONFIG", "") + if err != nil { + t.Fatal("Error setting PILOSA_CONFIG") + } + conf := NewConfig("test.yaml") + Convey("config.Get()", t, func() { + So(conf.Get("port_tcp"), ShouldEqual, 12000) + So(conf.Get("port_http"), ShouldEqual, 15000) + So(conf.Get("temp"), ShouldEqual, "/tmp") + So(conf.Get("notfound"), ShouldBeNil) + }) + Convey("config.GetSafe()", t, func() { + val, ok := conf.GetSafe("port_tcp") + So(ok, ShouldBeTrue) + So(val, ShouldEqual, 12000) + val, ok = conf.GetSafe("derp") + So(ok, ShouldBeFalse) + So(val, ShouldBeNil) + }) + Convey("config.GetInt()", t, func() { + So(conf.GetInt("port_tcp"), ShouldEqual, 12000) + So(conf.GetInt("port_http"), ShouldEqual, 15000) + So(conf.GetInt("notfound"), ShouldEqual, 0) + }) + Convey("config.GetString()", t, func() { + So(conf.GetString("temp"), ShouldEqual, "/tmp") + }) +}