warren/cmd/warrend/conf/deployments.go

66 lines
1.5 KiB
Go
Raw 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
2015-04-03 06:52:03 +00:00
func addDeployment(d *Msg) error {
if err := validateDeployment(d); err != nil {
return err
}
2015-04-03 06:52:03 +00:00
conf.Deployments[d.Args["name"]] = d.Args["path"]
dirToNick[d.Args["path"]] = d.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
2015-04-04 00:24:57 +00:00
if path,ok := deps[d.Args["arg"]]; ok {
// registered nick
delete(deps, d.Args["arg"])
delete(dirToNick, path)
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
// 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
}