1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2025-06-30 14:38:28 +00:00

major cleanup of config handling

This commit is contained in:
Vincent Batts 2013-08-05 23:06:05 -04:00
parent 25f42cf2be
commit cacdb3aaa5
3 changed files with 102 additions and 123 deletions

View file

@ -5,13 +5,23 @@ import (
"launchpad.net/goyaml"
)
type Config map[string]interface{}
type Config struct {
Server bool // Run as server, if different than false (server)
Ip string // Bind address, if different than 0.0.0.0 (server)
Port string // listen port, if different than '7777' (server)
MongoHost string // mongoDB host, if different than 'localhost' (server)
MongoDB string // mongoDB db name, if different than 'filesrv' (server)
MongoUsername string // mongoDB username, if any (server)
MongoPassword string // mongoDB password, if any (server)
RemoteHost string // imgsrv server to push files to (client)
Map map[string]interface{} // key/value options (not used currently)
}
// Of the configurations, provided option, return the value as a bool
func (c *Config) GetBool(option string) (value bool) {
conf := Config{}
conf = *c
switch conf[option] {
func (c Config) GetBool(option string) (value bool) {
switch c.Map[option] {
default:
value = false
case "yes", "on", "true":
@ -21,26 +31,31 @@ func (c *Config) GetBool(option string) (value bool) {
}
// Of the configurations, provided option, return the value as a string
func (c *Config) GetString(option string) (value string) {
conf := Config{}
conf = *c
value, _ = conf[option].(string)
func (c Config) GetString(option string) (value string) {
value, _ = c.Map[option].(string)
return
}
func (c *Config) Merge(other *Config) error {
if other == nil {
return nil
}
return nil
}
// Given a filename to a YAML file, unmarshal it, and return a Config
func ReadConfigFile(filename string) (config Config, err error) {
func ReadConfigFile(filename string) (*Config, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return
return nil, err
}
err = goyaml.Unmarshal(bytes, &config)
if err != nil {
return
config := Config{}
if err = goyaml.Unmarshal(bytes, &config); err != nil {
return nil, err
}
return config, nil
return &config, nil
}
/*