1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-30 12:05:42 +00:00
imgsrv/config/config.go

93 lines
2.2 KiB
Go
Raw Normal View History

package config
import (
2013-03-27 20:30:24 +00:00
"io/ioutil"
"launchpad.net/goyaml"
)
2013-08-06 03:06:05 +00:00
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)
2013-10-04 03:26:01 +00:00
MongoDbName string // mongoDB db name, if different than 'filesrv' (server)
2013-08-06 03:06:05 +00:00
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
2013-08-06 03:06:05 +00:00
func (c Config) GetBool(option string) (value bool) {
switch c.Map[option] {
2013-03-27 20:30:24 +00:00
default:
value = false
case "yes", "on", "true":
value = true
}
return
}
// Of the configurations, provided option, return the value as a string
2013-08-06 03:06:05 +00:00
func (c Config) GetString(option string) (value string) {
value, _ = c.Map[option].(string)
2013-03-27 20:30:24 +00:00
return
}
2013-08-06 03:06:05 +00:00
func (c *Config) Merge(other *Config) error {
2013-10-04 03:26:01 +00:00
if other == nil {
return nil
}
if other.Server {
c.Server = other.Server
}
if len(other.Ip) > 0 {
c.Ip = other.Ip
}
if len(other.Port) > 0 {
c.Port = other.Port
}
if len(other.Port) > 0 {
c.Port = other.Port
}
if len(other.MongoHost) > 0 {
c.MongoHost = other.MongoHost
}
if len(other.MongoDbName) > 0 {
c.MongoDbName = other.MongoDbName
}
if len(other.MongoUsername) > 0 {
c.MongoUsername = other.MongoUsername
}
if len(other.MongoPassword) > 0 {
c.MongoPassword = other.MongoPassword
}
if len(other.RemoteHost) > 0 && len(c.RemoteHost) == 0 {
2013-10-04 03:26:01 +00:00
c.RemoteHost = other.RemoteHost
}
return nil
2013-08-06 03:06:05 +00:00
}
// Given a filename to a YAML file, unmarshal it, and return a Config
2013-08-06 03:06:05 +00:00
func ReadConfigFile(filename string) (*Config, error) {
2013-03-27 20:30:24 +00:00
bytes, err := ioutil.ReadFile(filename)
if err != nil {
2013-08-06 03:06:05 +00:00
return nil, err
2013-03-27 20:30:24 +00:00
}
2013-08-06 03:06:05 +00:00
config := Config{}
if err = goyaml.Unmarshal(bytes, &config); err != nil {
return nil, err
2013-03-27 20:30:24 +00:00
}
2013-08-06 03:06:05 +00:00
return &config, nil
}
/*
func WriteConfigFile(filename string, data []byte) (err error) {
2013-03-27 20:30:24 +00:00
return
}
*/