Skip to content

Commit

Permalink
fix: improved logging returns
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Dec 10, 2024
1 parent f59d9f3 commit 4a6bcc6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Fixed-20241210-100157.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: Fixed logging returns when an error occurs
time: 2024-12-10T10:01:57.897306641+01:00
1 change: 0 additions & 1 deletion internal/cli/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 13 additions & 17 deletions internal/runner/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions internal/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 4a6bcc6

Please sign in to comment.