Skip to content

Commit

Permalink
Move handleError function to public namespace
Browse files Browse the repository at this point in the history
Other parts of the codebase will need to use the handleError
function so refactor it into the main debos namespace.

Signed-off-by: Christopher Obbard <[email protected]>
  • Loading branch information
obbardc committed Jan 13, 2024
1 parent b4ab707 commit d7cc0bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
13 changes: 13 additions & 0 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package debos

import (
"bytes"
"log"

"github.com/go-debos/fakemachine"
)

Expand Down Expand Up @@ -53,6 +55,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
Expand Down
29 changes: 9 additions & 20 deletions cmd/debos/debos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
log.Printf("==== %s ====\n", a)
Expand All @@ -37,13 +26,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
}
}
Expand Down Expand Up @@ -241,7 +230,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
}
}
Expand Down Expand Up @@ -319,11 +308,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
}
}
Expand All @@ -343,7 +332,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
}
}
Expand All @@ -359,11 +348,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
}
}
Expand All @@ -386,7 +375,7 @@ func main() {
if !fakemachine.InMachine() {
for _, a := range r.Actions {
err = a.PostMachine(&context)
if handleError(&context, err, a, "PostMachine") {
if debos.HandleError(&context, err, a, "PostMachine") {
return
}
}
Expand Down

0 comments on commit d7cc0bb

Please sign in to comment.