diff --git a/action.go b/action.go index 48eb6f96..b39fbf51 100644 --- a/action.go +++ b/action.go @@ -54,6 +54,17 @@ func (c *DebosContext) Origin(o string) (string, bool) { } } +func HandleError(context *DebosContext, err error, a Action, stage string) bool { + if err == nil { + return false + } + + context.State = Failed + log.Printf("Action `%s` failed at stage %s, error: %s", a, stage, err) + DebugShell(*context) + return true +} + type Action interface { /* FIXME verify should probably be prepare or somesuch */ Verify(context *DebosContext) error diff --git a/cmd/debos/debos.go b/cmd/debos/debos.go index b4e371c1..9eea5ef1 100644 --- a/cmd/debos/debos.go +++ b/cmd/debos/debos.go @@ -15,17 +15,6 @@ import ( "github.com/jessevdk/go-flags" ) -func handleError(context *debos.DebosContext, err error, a debos.Action, stage string) bool { - if err == nil { - return false - } - - context.State = debos.Failed - log.Printf("Action `%s` failed at stage %s, error: %s", a, stage, err) - debos.DebugShell(*context) - return true -} - func do_run(r actions.Recipe, context *debos.DebosContext) (success bool) { for _, a := range r.Actions { err := a.Run(context) @@ -36,13 +25,13 @@ func do_run(r actions.Recipe, context *debos.DebosContext) (success bool) { defer func(action debos.Action) { err := action.Cleanup(context) - if handleError(context, err, action, "Cleanup") { + if debos.HandleError(context, err, action, "Cleanup") { success = false } }(a) // Check the state of Run method - if handleError(context, err, a, "Run") { + if debos.HandleError(context, err, a, "Run") { return false } } @@ -240,7 +229,7 @@ func main() { for _, a := range r.Actions { err = a.Verify(&context) - if handleError(&context, err, a, "Verify") { + if debos.HandleError(&context, err, a, "Verify") { return } } @@ -318,11 +307,11 @@ func main() { err := action.PostMachineCleanup(&context) // report errors but do not stop execution - handleError(&context, err, action, "PostMachineCleanup") + debos.HandleError(&context, err, action, "PostMachineCleanup") }(a) err = a.PreMachine(&context, m, &args) - if handleError(&context, err, a, "PreMachine") { + if debos.HandleError(&context, err, a, "PreMachine") { return } } @@ -342,7 +331,7 @@ func main() { for _, a := range r.Actions { err = a.PostMachine(&context) - if handleError(&context, err, a, "PostMachine") { + if debos.HandleError(&context, err, a, "PostMachine") { return } } @@ -358,11 +347,11 @@ func main() { err := action.PostMachineCleanup(&context) // report errors but do not stop execution - handleError(&context, err, action, "PostMachineCleanup") + debos.HandleError(&context, err, action, "PostMachineCleanup") }(a) err = a.PreNoMachine(&context) - if handleError(&context, err, a, "PreNoMachine") { + if debos.HandleError(&context, err, a, "PreNoMachine") { return } } @@ -385,7 +374,7 @@ func main() { if !fakemachine.InMachine() { for _, a := range r.Actions { err = a.PostMachine(&context) - if handleError(&context, err, a, "PostMachine") { + if exitcode = debos.HandleError(&context, err, a, "PostMachine"); exitcode != 0 { return } }