warren/cmd/warrend/conf/deployments.go

65 lines
1.5 KiB
Go
Raw Permalink Normal View History

package conf
import (
"errors"
"os"
2015-04-04 00:24:57 +00:00
"github.com/dballard/warren/lib/warren"
)
// reverse look up map
var dirToNick map[string]string = make(map[string]string)
// given a valid deployment, add it
// internal, thread unsafe
func addDeployment(msg *Msg) error {
if err := validateDeployment(msg); err != nil {
return err
}
var dep Deployment
dep.Path = msg.Args["path"]
conf.Deployments[msg.Args["name"]] = dep
dirToNick[msg.Args["path"]] = msg.Args["name"]
saveConf()
return nil
}
2015-04-04 00:24:57 +00:00
func rmDeployment(d *Msg) error {
// Assume nick, check if registered
deps := GetConf().Deployments
if dep,ok := deps[d.Args["arg"]]; ok {
2015-04-04 00:24:57 +00:00
// registered nick
delete(deps, d.Args["arg"])
delete(dirToNick, dep.Path)
2015-04-04 00:24:57 +00:00
saveConf()
return nil
} else {
// Not a nick, try as path
absPath := warren.AbsPath(d.Args["path"], d.Args["arg"])
if nick,ok := dirToNick[absPath]; ok {
delete(deps, nick)
delete(dirToNick, absPath)
saveConf()
return nil
}
}
2015-04-04 00:24:57 +00:00
return errors.New("Cannot find deployment '" + d.Args["arg"] + "' to remove")
}
2015-04-04 00:24:57 +00:00
2015-04-03 06:52:03 +00:00
// Validate a deployment is ok to insert
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
}