diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index f68e419..f89ebc0 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -3,6 +3,11 @@ "GoVersion": "go1.6", "GodepVersion": "v62", "Deps": [ + { + "ImportPath": "github.com/Luzifer/go_helpers/str", + "Comment": "v1.3.0", + "Rev": "6abbbafaada02b63dd8f9e185921fd8b3c35b6c2" + }, { "ImportPath": "github.com/Luzifer/go_helpers/which", "Comment": "v1.3.0", diff --git a/configfile.go b/configfile.go index 90b1602..7c9155d 100644 --- a/configfile.go +++ b/configfile.go @@ -12,6 +12,23 @@ import ( "gopkg.in/yaml.v2" ) +const ( + commandBackup = "backup" + commandCleanup = "cleanup" + commandList = "list-current-files" + commandRestore = "restore" + commandStatus = "status" + commandVerify = "verify" + commandRemove = "__remove_old" +) + +var ( + notifyCommands = []string{ + commandBackup, + commandRemove, + } +) + type configFile struct { RootPath string `yaml:"root" valid:"required"` Hostname string `yaml:"hostname"` @@ -128,18 +145,18 @@ func (c *configFile) GenerateCommand(argv []string, time string) (commandLine [] ) switch command { - case "backup": + case commandBackup: option = "" root = c.RootPath dest = c.Destination commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, "") - case "cleanup": + case commandCleanup: option = command commandLine, env, err = c.generateLiteCommand(option, time, addTime) - case "list-current-files": + case commandList: option = command commandLine, env, err = c.generateLiteCommand(option, time, addTime) - case "restore": + case commandRestore: addTime = true option = command root = c.Destination @@ -156,15 +173,15 @@ func (c *configFile) GenerateCommand(argv []string, time string) (commandLine [] } commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, restoreFile) - case "status": + case commandStatus: option = "collection-status" commandLine, env, err = c.generateLiteCommand(option, time, addTime) - case "verify": + case commandVerify: option = command root = c.Destination dest = c.RootPath commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, "") - case "__remove_old": + case commandRemove: commandLine, env, err = c.generateRemoveCommand() default: err = fmt.Errorf("Did not understand command '%s', please see 'help' for details what to do.", command) diff --git a/main.go b/main.go index 0a38d97..5acd6f4 100644 --- a/main.go +++ b/main.go @@ -114,29 +114,18 @@ func main() { defer lock.Unlock() if err := execute(config, argv[1:]); err != nil { - if err := config.Notify(false, fmt.Errorf("Could not create backup: %s", err)); err != nil { - logf("[ERR] Error sending notifications: %s", err) - } else { - logf("[INF] Notifications sent") - } return } if config.Cleanup.Type != "none" { logf("++++ Starting removal of old backups") - if err := execute(config, []string{"__remove_old"}); err != nil { - if err := config.Notify(false, fmt.Errorf("Could not cleanup backup: %s", err)); err != nil { - logf("[ERR] Error sending notifications: %s", err) - } else { - logf("[INF] Notifications sent") - } - + if err := execute(config, []string{commandRemove}); err != nil { return } } - if err := config.Notify(true, nil); err != nil { + if err := config.Notify(argv[1], true, nil); err != nil { logf("[ERR] Error sending notifications: %s", err) } else { logf("[INF] Notifications sent") @@ -186,6 +175,14 @@ func execute(config *configFile, argv []string) error { logf("[INF] Execution of duplicity command was successful.") } + if err != nil { + if nErr := config.Notify(argv[0], false, fmt.Errorf("Could not create backup: %s", err)); nErr != nil { + logf("[ERR] Error sending notifications: %s", nErr) + } else { + logf("[INF] Notifications sent") + } + } + return err } diff --git a/notification.go b/notification.go index b8c7afa..760d692 100644 --- a/notification.go +++ b/notification.go @@ -5,9 +5,15 @@ import ( "encoding/json" "fmt" "net/http" + + "github.com/Luzifer/go_helpers/str" ) -func (c *configFile) Notify(success bool, err error) error { +func (c *configFile) Notify(command string, success bool, err error) error { + if !str.StringInSlice(command, notifyCommands) { + return nil + } + errs := []error{} for _, n := range []func(bool, error) error{ @@ -21,17 +27,17 @@ func (c *configFile) Notify(success bool, err error) error { if len(errs) == 0 { return nil - } else { - estr := "" - for _, e := range errs { - if e == nil { - continue - } - - estr = fmt.Sprintf("%s\n- %s", estr, e) - } - return fmt.Errorf("%d notifiers failed:%s", len(errs), estr) } + + estr := "" + for _, e := range errs { + if e == nil { + continue + } + + estr = fmt.Sprintf("%s\n- %s", estr, e) + } + return fmt.Errorf("%d notifiers failed:%s", len(errs), estr) } type mondashResult struct { @@ -64,7 +70,7 @@ func (c *configFile) notifyMonDash(success bool, err error) error { } buf := bytes.NewBuffer([]byte{}) - if err := json.NewEncoder(buf).Encode(monitoringResult); err != nil { + if err = json.NewEncoder(buf).Encode(monitoringResult); err != nil { return err } @@ -114,7 +120,7 @@ func (c *configFile) notifySlack(success bool, err error) error { } buf := bytes.NewBuffer([]byte{}) - if err := json.NewEncoder(buf).Encode(sr); err != nil { + if err = json.NewEncoder(buf).Encode(sr); err != nil { return err }