diff --git a/config.go b/config.go new file mode 100644 index 0000000..75abc73 --- /dev/null +++ b/config.go @@ -0,0 +1,44 @@ +package main + +import ( + "launchpad.net/goyaml" + "io/ioutil" +) + +type Config map[string]interface{} + +func (c *Config) GetBool(option string) (value bool) { + conf := Config{} + conf = *c + switch conf[option] { + default: value = false + case "yes", "on", "true": value = true + } + return +} + +func (c *Config) GetString(option string) (value string) { + conf := Config{} + conf = *c + value, _ = conf[option].(string) + return +} + +func ReadConfigFile(filename string) (config Config, err error) { + bytes, err := ioutil.ReadFile(filename) + if (err != nil) { + return + } + + err = goyaml.Unmarshal(bytes, &config) + if (err != nil) { + return + } + + return config, nil +} + +func WriteConfigFile(filename string, data []byte) (err error) { + return +} + diff --git a/get.go b/get.go new file mode 100644 index 0000000..84c919e --- /dev/null +++ b/get.go @@ -0,0 +1,31 @@ +package main + +import ( + "labix.org/v2/mgo/bson" +) + +/* gfs is a *mgo.GridFS defined in imgsrv.go */ + +func GetFileByFilename(filename string) (this_file File, err error) { + err = gfs.Find(bson.M{"filename":filename}).One(&this_file) + if (err != nil) { + return this_file, err + } + return this_file, nil +} + +func GetFileRandom() (this_file File, err error) { + r := rand64() + err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file) + if (err != nil) { + return this_file, err + } + if (len(this_file.Md5) == 0) { + err = gfs.Find(bson.M{"random": bson.M{"$lt" : r } }).One(&this_file) + } + if (err != nil) { + return this_file, err + } + return this_file, nil +} + diff --git a/imgsrv.go b/imgsrv.go index 3984c9e..0234b81 100644 --- a/imgsrv.go +++ b/imgsrv.go @@ -11,7 +11,6 @@ import ( "log" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" - goconf "git.corp.redhat.com/srv/git/dev/towers/engineering/go/conf.git" "net/http" "flag" "fmt" @@ -32,7 +31,7 @@ import ( ) var ( - ConfigFile = fmt.Sprintf("%s/.imgsrv.conf", os.Getenv("HOME")) + ConfigFile = fmt.Sprintf("%s/.imgsrv.yaml", os.Getenv("HOME")) DefaultRunAsServer = false RunAsServer = DefaultRunAsServer @@ -109,29 +108,6 @@ func hasFileByKeyword(keyword string) (exists bool, err error) { return exists, nil } -func getFileByFilename(filename string) (this_file File, err error) { - err = gfs.Find(bson.M{"filename":filename}).One(&this_file) - if (err != nil) { - return this_file, err - } - return this_file, nil -} - -func getFileRandom() (this_file File, err error) { - r := rand64() - err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file) - if (err != nil) { - return this_file, err - } - if (len(this_file.Md5) == 0) { - err = gfs.Find(bson.M{"random": bson.M{"$lt" : r } }).One(&this_file) - } - if (err != nil) { - return this_file, err - } - return this_file, nil -} - func putFileFromPath(host, filename string) (path string, err error) { ext := filepath.Ext(filename) @@ -675,22 +651,22 @@ func init() { } -func loadConfiguration(filename string) (c *goconf.ConfigFile) { +func loadConfiguration(filename string) (c Config) { //log.Printf("Attempting to load config file: %s", filename) - c, err := goconf.ReadConfigFile(filename) + c, err := ReadConfigFile(filename) if (err != nil) { //log.Println(err) - return goconf.NewConfigFile() + return Config{} } - cRunAsServer, _ := c.GetBool("default", "server") - cServerIp, _ := c.GetString("default", "ip") - cServerPort, _ := c.GetString("default", "port") - cMongoHost, _ := c.GetString("default", "mongohost") - cMongoDB, _ := c.GetString("default", "mongodb") - cMongoUsername, _ := c.GetString("default", "mongousername") - cMongoPassword, _ := c.GetString("default", "mongopassword") - cRemoteHost, _ := c.GetString("default", "remotehost") + cRunAsServer := c.GetBool("server") + cServerIp := c.GetString("ip") + cServerPort := c.GetString("port") + cMongoHost := c.GetString("mongohost") + cMongoDB := c.GetString("mongodb") + cMongoUsername := c.GetString("mongousername") + cMongoPassword := c.GetString("mongopassword") + cRemoteHost := c.GetString("remotehost") // Only set variables from config file, // if they weren't passed as flags