warren/cmd/warrend/conf/conf.go

118 lines
2.2 KiB
Go
Raw Normal View History

package conf
import (
"encoding/json"
"errors"
"fmt"
"github.com/dballard/warren/lib/warren"
"os"
)
type MsgType int
const (
AddDeployment MsgType = iota
RmDeploymen
)
type Msg struct {
mType MsgType
reply chan error
args map[string]string
}
func NewMsg(mType MsgType, args map[string]string) *Msg {
var m = &Msg{mType, make(chan error), args}
return m
}
func (m *Msg) Call() error {
confChan <- *m
err := <-m.reply
return err
}
// 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 confChan = make(chan Msg)
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 ni l
}
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 {
m := <- confChan
select (m.mType) {
case dm := <-addDeploymentChan:
addDeployment(&dm)
}
}
}