Add config module.

This commit is contained in:
Cody Soyland 2013-11-22 17:22:53 -06:00
parent 258c990fd1
commit 2aade5c2a9
2 changed files with 58 additions and 0 deletions

53
config/config.go Normal file
View file

@ -0,0 +1,53 @@
package config
import (
"flag"
"log"
"io/ioutil"
"launchpad.net/goyaml"
)
var config map[string]interface{}
func GetSafe(key string) (interface{}, bool) {
value, ok := config[key]
return value, ok
}
func Get(key string) interface{} {
return config[key]
}
func GetInt(key string) int {
value, ok := GetSafe(key)
if ok {
value_int, ok := value.(int)
if ok {
return value_int
}
}
return 0
}
func GetString(key string) string {
value, ok := GetSafe(key)
if ok {
value_string, ok := value.(string)
if ok {
return value_string
}
}
return ""
}
func init() {
log.Println("Start config")
config = make(map[string]interface{})
config_file := flag.String("config", "pilosa.yaml", "Path to config file.")
flag.Parse()
data, err := ioutil.ReadFile(*config_file)
if err != nil {
log.Fatal("Problem with config file. ", err)
}
goyaml.Unmarshal(data, &config)
}

View file

@ -19,6 +19,11 @@
"version": "87bcc4729f2c5a08d2513ad10684c6bbd256380f",
"type": "git"
},
"goyaml": {
"repo": "launchpad.net/goyaml",
"version": "50",
"type": "bzr"
},
"lru": {
"repo": "github.com/golang/groupcache/lru",
"version": "d781998583680cda80cf61e0b37dd0cd8da2eb52",