warren/cmd/warrend/conf/deployments.go

63 lines
1.5 KiB
Go

package conf
import (
"errors"
"os"
"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(d *Msg) error {
if err := validateDeployment(d); err != nil {
return err
}
conf.Deployments[d.Args["name"]] = d.Args["path"]
dirToNick[d.Args["path"]] = d.Args["name"]
saveConf()
return nil
}
func rmDeployment(d *Msg) error {
// Assume nick, check if registered
deps := GetConf().Deployments
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
}
}
return errors.New("Cannot find deployment '" + d.Args["arg"] + "' to remove")
}
// 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
}