Skip to content

Commit 64240dd

Browse files
committed
fix: address review comments on error swallowing and spinner handling
Signed-off-by: Enrique Lacal <enrique.lacal@kaleido.io>
1 parent f1bab89 commit 64240dd

6 files changed

Lines changed: 34 additions & 26 deletions

File tree

cmd/init_cardano.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ var initCardanoCmd = &cobra.Command{
4949
return err
5050
}
5151
if err := stackManager.InitStack(&initOptions); err != nil {
52-
// Try to clean up, but don't mask the original error
53-
_ = stackManager.RemoveStack()
52+
// Try to clean up, but log rather than return any error to not mask the original error
53+
if verr := stackManager.RemoveStack(); verr != nil {
54+
l := log.LoggerFromContext(ctx)
55+
l.Info(fmt.Sprintf("Error whilst removing the stack: %s", verr.Error()))
56+
}
5457
return err
5558
}
5659
fmt.Printf("Stack '%s' created!\nTo start your new stack run:\n\n%s start %s\n", initOptions.StackName, rootCmd.Use, initOptions.StackName)

cmd/init_fabric.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ var initFabricCmd = &cobra.Command{
5454
return err
5555
}
5656
if err := stackManager.InitStack(&initOptions); err != nil {
57-
// Try to clean up, but don't mask the original error
58-
_ = stackManager.RemoveStack()
57+
// Try to clean up, but log rather than return any error to not mask the original error
58+
if verr := stackManager.RemoveStack(); verr != nil {
59+
l := log.LoggerFromContext(ctx)
60+
l.Info(fmt.Sprintf("Error whilst removing the stack: %s", verr.Error()))
61+
}
5962
return err
6063
}
6164
fmt.Printf("Stack '%s' created!\nTo start your new stack run:\n\n%s start %s\n", initOptions.StackName, rootCmd.Use, initOptions.StackName)

cmd/init_tezos.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ var initTezosCmd = &cobra.Command{
5050
return err
5151
}
5252
if err := stackManager.InitStack(&initOptions); err != nil {
53-
// Try to clean up, but don't mask the original error
54-
_ = stackManager.RemoveStack()
53+
// Try to clean up, but log rather than return any error to not mask the original error
54+
if verr := stackManager.RemoveStack(); verr != nil {
55+
l := log.LoggerFromContext(ctx)
56+
l.Info(fmt.Sprintf("Error whilst removing the stack: %s", verr.Error()))
57+
}
5558
return err
5659
}
5760
fmt.Printf("Stack '%s' created!\nTo start your new stack run:\n\n%s start %s\n", initOptions.StackName, rootCmd.Use, initOptions.StackName)

cmd/pull.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,9 @@ Pull the images for a stack .
6363
if err := stackManager.LoadStack(stackName); err != nil {
6464
return err
6565
}
66-
if spin != nil {
67-
spin.Start()
68-
}
69-
err = stackManager.PullStack(&pullOptions)
70-
if spin != nil {
71-
spin.Stop()
72-
}
73-
// Throw an error after stopping the spin, this will prevent the user's terminal from having the spinner as overlay
74-
if err != nil {
75-
return err
76-
}
77-
return nil
66+
return withSpinner(spin, func() error {
67+
return stackManager.PullStack(&pullOptions)
68+
})
7869
},
7970
}
8071

cmd/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323

24+
"github.com/briandowns/spinner"
2425
"github.com/mattn/go-isatty"
2526
"github.com/spf13/cobra"
2627

@@ -42,6 +43,16 @@ var logger log.Logger = &log.StdoutLogger{
4243
// name of the executable, this is for the help messages
4344
var ExecutableName string = os.Args[0]
4445

46+
// withSpinner runs fn with the spinner (if configured) running, and stops it
47+
// before returning so an error is not overlaid by the spinner in the user's terminal
48+
func withSpinner(spin *spinner.Spinner, fn func() error) error {
49+
if spin != nil {
50+
spin.Start()
51+
defer spin.Stop()
52+
}
53+
return fn()
54+
}
55+
4556
func GetFireflyASCIIArt() string {
4657
s := ""
4758
s += "\u001b[33m _______ ________ \u001b[0m\n" // yellow

cmd/start.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,11 @@ This command will start a stack and run it in the background.
7171
fmt.Println("this will take a few seconds longer since this is the first time you're running this stack...")
7272
}
7373

74-
if spin != nil {
75-
spin.Start()
76-
}
77-
messages, err := stackManager.StartStack(&startOptions)
78-
if spin != nil {
79-
spin.Stop()
80-
}
81-
if err != nil {
74+
var messages []string
75+
if err := withSpinner(spin, func() (err error) {
76+
messages, err = stackManager.StartStack(&startOptions)
77+
return err
78+
}); err != nil {
8279
return err
8380
}
8481
fmt.Print("\n\n")

0 commit comments

Comments
 (0)