new messeging flow done

This commit is contained in:
Dan Ballard 2015-04-02 23:52:03 -07:00
parent c3c6bee7a8
commit be9e65bcd7
3 changed files with 45 additions and 60 deletions

View File

@ -40,26 +40,29 @@ func regRun(cmd *SockCommand, c net.Conn, path string, args ...string) {
} }
// get either supplied nick or parse from dir // get either supplied nick or parse from dir
d := new(conf.DeploymentMsg) d := conf.NewMsg(conf.AddDeployment)
d.Path = warren.AbsPath(path, args[0])
d.Args["path"] = warren.AbsPath(path, args[0])
if len(args) >= 2 && strings.TrimSpace(args[1]) != "" { if len(args) >= 2 && strings.TrimSpace(args[1]) != "" {
d.Name = args[1] d.Args["name"] = args[1]
} else { } else {
parts := strings.Split(strings.Trim(d.Path, " /"), "/") parts := strings.Split(strings.Trim(d.Args["path"], " /"), "/")
if parts[len(parts)-1] == "deploy" { if parts[len(parts)-1] == "deploy" {
d.Name = parts[len(parts)-2] d.Args["name"] = parts[len(parts)-2]
} else { } else {
d.Name = parts[len(parts)-1] d.Args["name"] = parts[len(parts)-1]
} }
} }
err := conf.AddDeployment(d)
err := d.Call()
if err != nil { if err != nil {
warren.WriteStringz(c, "ERROR: "+err.Error()) warren.WriteStringz(c, "ERROR: "+err.Error())
return return
} }
warren.WriteStringz(c, "Registered deployment "+d.Name+" at "+d.Path) warren.WriteStringz(c, "Registered deployment "+d.Args["name"]+ " at "+d.Args["path"])
} }
// unreg path // unreg path
@ -70,11 +73,11 @@ func unregRun(cmd *SockCommand, c net.Conn, path string, args ...string) {
return return
} }
err := conf.RmDeployment(args[0]) /*err := conf.RmDeployment(args[0])
if err != nil { if err != nil {
warren.WriteStringz(c, "ERROR: " + err.Error()) warren.WriteStringz(c, "ERROR: " + err.Error())
return return
} }*/
warren.WriteStringz(c, "Unregistered deployment " + args[0]) warren.WriteStringz(c, "Unregistered deployment " + args[0])
} }

View File

@ -17,11 +17,11 @@ const (
type Msg struct { type Msg struct {
mType MsgType mType MsgType
reply chan error reply chan error
args map[string]string Args map[string]string
} }
func NewMsg(mType MsgType, args map[string]string) *Msg { func NewMsg(mType MsgType) *Msg {
var m = &Msg{mType, make(chan error), args} var m = &Msg{mType, make(chan error), make(map[string]string)}
return m return m
} }
@ -108,9 +108,9 @@ func Run() {
for { for {
m := <- confChan m := <- confChan
select (m.mType) { switch (m.mType) {
case dm := <-addDeploymentChan: case AddDeployment:
addDeployment(&dm) addDeployment(&m)
} }
} }

View File

@ -5,62 +5,25 @@ import (
"os" "os"
) )
type DeploymentMsg struct {
Name string
Path string
Reply chan error
}
// reverse look up map // reverse look up map
var dirToNick map[string]string = make(map[string]string) var dirToNick map[string]string = make(map[string]string)
// Validate a deployment is ok to insert
// internal thread unsafe
func validateDeployment(d *DeploymentMsg) error {
// check if exists
if _, ok := conf.Deployments[d.Name]; ok {
return errors.New("Deployment with that name already exists")
}
if r, ok := dirToNick[d.Path]; ok {
return errors.New("Directory with that name already registered under nick '" + r + "'")
}
// check if dir exists
if _, err := os.Stat(d.Path); err != nil {
return err
}
return nil
}
// given a valid deployment, add it // given a valid deployment, add it
// internal, thread unsafe // internal, thread unsafe
func addDeployment(d *DeploymentMsg) error { func addDeployment(d *Msg) error {
if err := validateDeployment(d); err != nil { if err := validateDeployment(d); err != nil {
d.Reply <- err d.reply <- err
return err return err
} }
conf.Deployments[d.Name] = d.Path conf.Deployments[d.Args["name"]] = d.Args["path"]
dirToNick[d.Path] = d.Name dirToNick[d.Args["path"]] = d.Args["name"]
saveConf() saveConf()
d.Reply <- nil d.reply <- nil
return nil return nil
} }
/*
// conf API interface to add a deployment, vets it
func AddDeployment(d *DeploymentMsg) error {
d.Reply = make(chan error)
// add to conf / save
addDeploymentChan <- *d
err := <-d.Reply
return err
// TODO: register with runner, like conf would
return nil
}
func RmDeployment(string arg) error { func RmDeployment(string arg) error {
reply = make(chan error) reply = make(chan error)
rmDeploymentChan <- rmDeploymentChan <-
@ -74,3 +37,22 @@ func RmDeployment(string arg) error {
// if _,ok := conf. // if _,ok := conf.
} }
} }
*/
// Validate a deployment is ok to insert
// internal thread unsafe
func validateDeployment(d *Msg) error {
// check if exists
if _, ok := conf.Deployments[d.Args["Name"]]; ok {
return errors.New("Deployment with that name already exists")
}
if r, ok := dirToNick[d.Args["path"]]; ok {
return errors.New("Directory with that name already registered under nick '" + r + "'")
}
// check if dir exists
if _, err := os.Stat(d.Args["path"]); err != nil {
return err
}
return nil
}