From 4a6bcc6d3f0fda864529f9cd31e9a80e6021924a Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 10 Dec 2024 10:02:42 +0100 Subject: [PATCH] fix: improved logging returns --- .../unreleased/Fixed-20241210-100157.yaml | 3 ++ internal/cli/errors.go | 1 - internal/runner/graph.go | 30 ++++++++----------- internal/utils/exec.go | 5 ++-- 4 files changed, 18 insertions(+), 21 deletions(-) create mode 100644 .changes/unreleased/Fixed-20241210-100157.yaml diff --git a/.changes/unreleased/Fixed-20241210-100157.yaml b/.changes/unreleased/Fixed-20241210-100157.yaml new file mode 100644 index 00000000..03eca23e --- /dev/null +++ b/.changes/unreleased/Fixed-20241210-100157.yaml @@ -0,0 +1,3 @@ +kind: Fixed +body: Fixed logging returns when an error occurs +time: 2024-12-10T10:01:57.897306641+01:00 diff --git a/internal/cli/errors.go b/internal/cli/errors.go index 507d4c75..2b1c1c1a 100644 --- a/internal/cli/errors.go +++ b/internal/cli/errors.go @@ -55,7 +55,6 @@ func PrintExitError(summary string, detail ...string) { } func HandleErr(err error) { - log.Error().Msgf("Error: %v\n", err) var openApiErr *mccsdk.GenericOpenAPIError if errors.As(err, &openApiErr) { remoteErr := openApiErr.Model() diff --git a/internal/runner/graph.go b/internal/runner/graph.go index 8b2cbd45..15ea6f56 100644 --- a/internal/runner/graph.go +++ b/internal/runner/graph.go @@ -108,10 +108,6 @@ func (gr *GraphRunner) run(ctx context.Context, g *graph.Graph, f executorFunc, errChan <- err return } - - if err != nil { - errChan <- err - } }(ctx, n) } wg.Wait() @@ -167,7 +163,7 @@ func (gr *GraphRunner) TerraformApply(ctx context.Context, dg *graph.Graph, opts out, err := terraform.Apply(ctx, n.Path(), aOpts...) if err != nil { - return err + err = fmt.Errorf("failed to apply %s: %w", n.Identifier(), err) } if cli.OutputFromContext(ctx) == cli.OutputTypeJSON { @@ -186,12 +182,12 @@ func (gr *GraphRunner) TerraformApply(ctx context.Context, dg *graph.Graph, opts log.Ctx(ctx).Info().Msg(out) } - log.Ctx(ctx).Info().Msgf("Storing new hash for %s", n.Path()) + log.Ctx(ctx).Debug().Msgf("Storing new hash for %s", n.Path()) if err := gr.hash.Store(ctx, n); err != nil { log.Ctx(ctx).Warn().Err(err).Msgf("Failed to store hash for %s", n.Identifier()) } - return nil + return err }, opts.IgnoreChangeDetection); err != nil { return err @@ -215,11 +211,11 @@ func (gr *GraphRunner) TerraformValidate(ctx context.Context, dg *graph.Graph) e out, err = terraform.Validate(ctx, n.Path(), vOpts...) if err != nil { - return err + err = fmt.Errorf("failed to validate %s: %w", n.Identifier(), err) } log.Ctx(ctx).Info().Msg(out) - return nil + return err }, true) } @@ -256,7 +252,7 @@ func (gr *GraphRunner) TerraformPlan(ctx context.Context, dg *graph.Graph, opts out, err := terraform.Plan(ctx, n.Path(), pOpts...) if err != nil { - return err + err = fmt.Errorf("failed to plan %s: %w", n.Identifier(), err) } if cli.OutputFromContext(ctx) == cli.OutputTypeJSON { @@ -275,7 +271,7 @@ func (gr *GraphRunner) TerraformPlan(ctx context.Context, dg *graph.Graph, opts log.Ctx(ctx).Info().Msg(out) } - return nil + return err }, opts.IgnoreChangeDetection); err != nil { return err } @@ -291,10 +287,10 @@ func (gr *GraphRunner) TerraformProxy(ctx context.Context, dg *graph.Graph, opts out, err := utils.RunTerraform(ctx, n.Path(), opts.Command...) if err != nil { - return err + err = fmt.Errorf("failed to proxy %s: %w", n.Identifier(), err) } log.Ctx(ctx).Info().Msg(out) - return nil + return err }, opts.IgnoreChangeDetection); err != nil { return err } @@ -325,7 +321,7 @@ func (gr *GraphRunner) TerraformShow(ctx context.Context, dg *graph.Graph, opts out, err := terraform.Show(ctx, n.Path(), sOpts...) if err != nil { - return err + err = fmt.Errorf("failed to show %s: %w", n.Identifier(), err) } if cli.OutputFromContext(ctx) == cli.OutputTypeJSON { @@ -343,7 +339,7 @@ func (gr *GraphRunner) TerraformShow(ctx context.Context, dg *graph.Graph, opts } else { log.Ctx(ctx).Info().Msg(out) } - return nil + return err }, opts.IgnoreChangeDetection); err != nil { return err } @@ -355,10 +351,10 @@ func (gr *GraphRunner) TerraformInit(ctx context.Context, dg *graph.Graph) error if err := gr.run(ctx, dg, func(ctx context.Context, n graph.Node) error { out, err := terraform.Init(ctx, n.Path()) if err != nil { - return err + err = fmt.Errorf("failed to init %s: %w", n.Identifier(), err) } log.Ctx(ctx).Info().Msg(out) - return nil + return err }, true); err != nil { return err } diff --git a/internal/utils/exec.go b/internal/utils/exec.go index a912e5fd..7e2a4bb7 100644 --- a/internal/utils/exec.go +++ b/internal/utils/exec.go @@ -28,7 +28,7 @@ func RunInteractive(ctx context.Context, command string, cwd string, args ...str stdOut := new(bytes.Buffer) cmd.Stdin = os.Stdin - cmd.Stderr = os.Stderr + cmd.Stderr = stdOut cmd.Stdout = stdOut err := cmd.Start() @@ -48,8 +48,7 @@ func RunInteractive(ctx context.Context, command string, cwd string, args ...str case err := <-done: if err != nil { - //TODO: should we return the buffer here also? - return "", fmt.Errorf("command (%s) failed: %w (args: %s , cwd: %s)", command, err, strings.Join(args, " "), cwd) + return stdOut.String(), fmt.Errorf("command (%s) failed: %w (args: %s , cwd: %s)", command, err, strings.Join(args, " "), cwd) } }