65 lines
1.5 KiB
Go
65 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(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
|
|
}
|
|
|
|
func rmDeployment(d *Msg) error {
|
|
// Assume nick, check if registered
|
|
deps := GetConf().Deployments
|
|
if dep,ok := deps[d.Args["arg"]]; ok {
|
|
// registered nick
|
|
delete(deps, d.Args["arg"])
|
|
delete(dirToNick, dep.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
|
|
} |