Skip to content

Commit bfc5687

Browse files
authored
fix: improved logging returns (#466)
<!-- Describe in detail the changes you are proposing, and the rationale. --> <!-- Link all GitHub issues fixed by this PR, and add references to prior related PRs. --> Fixes # ### NEW FEATURES | UPGRADE NOTES | ENHANCEMENTS | BUG FIXES | EXPERIMENTS <!-- Write a short description of your changes. Examples: - Fixed a bug - Added a new feature - Updated documentation --> -
2 parents f59d9f3 + 4a6bcc6 commit bfc5687

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: Fixed logging returns when an error occurs
3+
time: 2024-12-10T10:01:57.897306641+01:00

internal/cli/errors.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func PrintExitError(summary string, detail ...string) {
5555
}
5656

5757
func HandleErr(err error) {
58-
log.Error().Msgf("Error: %v\n", err)
5958
var openApiErr *mccsdk.GenericOpenAPIError
6059
if errors.As(err, &openApiErr) {
6160
remoteErr := openApiErr.Model()

internal/runner/graph.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ func (gr *GraphRunner) run(ctx context.Context, g *graph.Graph, f executorFunc,
108108
errChan <- err
109109
return
110110
}
111-
112-
if err != nil {
113-
errChan <- err
114-
}
115111
}(ctx, n)
116112
}
117113
wg.Wait()
@@ -167,7 +163,7 @@ func (gr *GraphRunner) TerraformApply(ctx context.Context, dg *graph.Graph, opts
167163

168164
out, err := terraform.Apply(ctx, n.Path(), aOpts...)
169165
if err != nil {
170-
return err
166+
err = fmt.Errorf("failed to apply %s: %w", n.Identifier(), err)
171167
}
172168

173169
if cli.OutputFromContext(ctx) == cli.OutputTypeJSON {
@@ -186,12 +182,12 @@ func (gr *GraphRunner) TerraformApply(ctx context.Context, dg *graph.Graph, opts
186182
log.Ctx(ctx).Info().Msg(out)
187183
}
188184

189-
log.Ctx(ctx).Info().Msgf("Storing new hash for %s", n.Path())
185+
log.Ctx(ctx).Debug().Msgf("Storing new hash for %s", n.Path())
190186
if err := gr.hash.Store(ctx, n); err != nil {
191187
log.Ctx(ctx).Warn().Err(err).Msgf("Failed to store hash for %s", n.Identifier())
192188
}
193189

194-
return nil
190+
return err
195191

196192
}, opts.IgnoreChangeDetection); err != nil {
197193
return err
@@ -215,11 +211,11 @@ func (gr *GraphRunner) TerraformValidate(ctx context.Context, dg *graph.Graph) e
215211

216212
out, err = terraform.Validate(ctx, n.Path(), vOpts...)
217213
if err != nil {
218-
return err
214+
err = fmt.Errorf("failed to validate %s: %w", n.Identifier(), err)
219215
}
220216
log.Ctx(ctx).Info().Msg(out)
221217

222-
return nil
218+
return err
223219
}, true)
224220
}
225221

@@ -256,7 +252,7 @@ func (gr *GraphRunner) TerraformPlan(ctx context.Context, dg *graph.Graph, opts
256252

257253
out, err := terraform.Plan(ctx, n.Path(), pOpts...)
258254
if err != nil {
259-
return err
255+
err = fmt.Errorf("failed to plan %s: %w", n.Identifier(), err)
260256
}
261257

262258
if cli.OutputFromContext(ctx) == cli.OutputTypeJSON {
@@ -275,7 +271,7 @@ func (gr *GraphRunner) TerraformPlan(ctx context.Context, dg *graph.Graph, opts
275271
log.Ctx(ctx).Info().Msg(out)
276272
}
277273

278-
return nil
274+
return err
279275
}, opts.IgnoreChangeDetection); err != nil {
280276
return err
281277
}
@@ -291,10 +287,10 @@ func (gr *GraphRunner) TerraformProxy(ctx context.Context, dg *graph.Graph, opts
291287

292288
out, err := utils.RunTerraform(ctx, n.Path(), opts.Command...)
293289
if err != nil {
294-
return err
290+
err = fmt.Errorf("failed to proxy %s: %w", n.Identifier(), err)
295291
}
296292
log.Ctx(ctx).Info().Msg(out)
297-
return nil
293+
return err
298294
}, opts.IgnoreChangeDetection); err != nil {
299295
return err
300296
}
@@ -325,7 +321,7 @@ func (gr *GraphRunner) TerraformShow(ctx context.Context, dg *graph.Graph, opts
325321

326322
out, err := terraform.Show(ctx, n.Path(), sOpts...)
327323
if err != nil {
328-
return err
324+
err = fmt.Errorf("failed to show %s: %w", n.Identifier(), err)
329325
}
330326

331327
if cli.OutputFromContext(ctx) == cli.OutputTypeJSON {
@@ -343,7 +339,7 @@ func (gr *GraphRunner) TerraformShow(ctx context.Context, dg *graph.Graph, opts
343339
} else {
344340
log.Ctx(ctx).Info().Msg(out)
345341
}
346-
return nil
342+
return err
347343
}, opts.IgnoreChangeDetection); err != nil {
348344
return err
349345
}
@@ -355,10 +351,10 @@ func (gr *GraphRunner) TerraformInit(ctx context.Context, dg *graph.Graph) error
355351
if err := gr.run(ctx, dg, func(ctx context.Context, n graph.Node) error {
356352
out, err := terraform.Init(ctx, n.Path())
357353
if err != nil {
358-
return err
354+
err = fmt.Errorf("failed to init %s: %w", n.Identifier(), err)
359355
}
360356
log.Ctx(ctx).Info().Msg(out)
361-
return nil
357+
return err
362358
}, true); err != nil {
363359
return err
364360
}

internal/utils/exec.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func RunInteractive(ctx context.Context, command string, cwd string, args ...str
2828
stdOut := new(bytes.Buffer)
2929

3030
cmd.Stdin = os.Stdin
31-
cmd.Stderr = os.Stderr
31+
cmd.Stderr = stdOut
3232
cmd.Stdout = stdOut
3333

3434
err := cmd.Start()
@@ -48,8 +48,7 @@ func RunInteractive(ctx context.Context, command string, cwd string, args ...str
4848

4949
case err := <-done:
5050
if err != nil {
51-
//TODO: should we return the buffer here also?
52-
return "", fmt.Errorf("command (%s) failed: %w (args: %s , cwd: %s)", command, err, strings.Join(args, " "), cwd)
51+
return stdOut.String(), fmt.Errorf("command (%s) failed: %w (args: %s , cwd: %s)", command, err, strings.Join(args, " "), cwd)
5352
}
5453
}
5554

0 commit comments

Comments
 (0)