package conf import ( "encoding/json" "errors" "fmt" "github.com/dballard/warren/lib/warren" "os" ) // Conf package - thread safe conf // All write ops are public API functions // that internall use channels to thread safely queue writes // all write ops cause FS conf writes so that state is preserved // Reads return copies of data type Conf struct { // name : path Deployments map[string]string } var conf *Conf = nil var Init = make(chan bool) var addDeploymentChan = make(chan DeploymentMsg) func loadConf() { file, err := os.Open(warren.WarrendConf) if err != nil { fmt.Println("Error loading ", warren.WarrendConf, ": ", err) return } defer file.Close() decoder := json.NewDecoder(file) conf = new(Conf) err = decoder.Decode(conf) if err != nil { conf = nil fmt.Println("Error parsing ", warren.WarrendConf, ": ", err) } if conf.Deployments == nil { conf.Deployments = make(map[string]string) } // populate reverse map for nick, dir := range conf.Deployments { dirToNick[dir] = nick } } func saveConf() error { file, err := os.Create(warren.WarrendConf) if err != nil { // TODO: Um, more than noting we might want to do something more drastic return errors.New("Error: could not open conf file for saving: " + err.Error()) } defer file.Close() jdata, err := json.MarshalIndent(conf, "", "\t") if err != nil { return errors.New("Error: Could not enocde to json: " + err.Error()) } if _, err := file.Write(jdata); err != nil { return errors.New("Error: Could not write to file: " + err.Error()) } return nil } func GetConf() Conf { return *conf } func Run() { loadConf() if conf == nil { fmt.Println("failed to load conf") Init <- false } // set up comm chans Init <- true // loop for Writes for { select { case dm := <-addDeploymentChan: addDeployment(&dm) } } }