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
d := new(conf.DeploymentMsg)
d.Path = warren.AbsPath(path, args[0])
d := conf.NewMsg(conf.AddDeployment)
d.Args["path"] = warren.AbsPath(path, args[0])
if len(args) >= 2 && strings.TrimSpace(args[1]) != "" {
d.Name = args[1]
d.Args["name"] = args[1]
} else {
parts := strings.Split(strings.Trim(d.Path, " /"), "/")
parts := strings.Split(strings.Trim(d.Args["path"], " /"), "/")
if parts[len(parts)-1] == "deploy" {
d.Name = parts[len(parts)-2]
d.Args["name"] = parts[len(parts)-2]
} else {
d.Name = parts[len(parts)-1]
d.Args["name"] = parts[len(parts)-1]
}
}
err := conf.AddDeployment(d)
err := d.Call()
if err != nil {
warren.WriteStringz(c, "ERROR: "+err.Error())
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
@ -70,11 +73,11 @@ func unregRun(cmd *SockCommand, c net.Conn, path string, args ...string) {
return
}
err := conf.RmDeployment(args[0])
/*err := conf.RmDeployment(args[0])
if err != nil {
warren.WriteStringz(c, "ERROR: " + err.Error())
return
}
}*/
warren.WriteStringz(c, "Unregistered deployment " + args[0])
}

View File

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

View File

@ -5,62 +5,25 @@ import (
"os"
)
type DeploymentMsg struct {
Name string
Path string
Reply chan error
}
// reverse look up map
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
// internal, thread unsafe
func addDeployment(d *DeploymentMsg) error {
func addDeployment(d *Msg) error {
if err := validateDeployment(d); err != nil {
d.Reply <- err
d.reply <- err
return err
}
conf.Deployments[d.Name] = d.Path
dirToNick[d.Path] = d.Name
conf.Deployments[d.Args["name"]] = d.Args["path"]
dirToNick[d.Args["path"]] = d.Args["name"]
saveConf()
d.Reply <- nil
d.reply <- 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 {
reply = make(chan error)
rmDeploymentChan <-
@ -74,3 +37,22 @@ func RmDeployment(string arg) error {
// 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
}