Skip to content

Commit e2246b4

Browse files
committed
Fix usage of errors.Join
Signed-off-by: Akihiro Suda <[email protected]>
1 parent e788c08 commit e2246b4

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

pkg/hostagent/hostagent.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
399399
}
400400
return nil
401401
})
402-
var mErr error
402+
var errs []error
403403
if err := a.waitForRequirements("essential", a.essentialRequirements()); err != nil {
404-
mErr = errors.Join(mErr, err)
404+
errs = append(errs, err)
405405
}
406406
if *a.y.SSH.ForwardAgent {
407407
faScript := `#!/bin/bash
@@ -413,67 +413,67 @@ sudo chown -R "${USER}" /run/host-services`
413413
stdout, stderr, err := ssh.ExecuteScript("127.0.0.1", a.sshLocalPort, a.sshConfig, faScript, faDesc)
414414
logrus.Debugf("stdout=%q, stderr=%q, err=%v", stdout, stderr, err)
415415
if err != nil {
416-
mErr = errors.Join(mErr, fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err))
416+
errs = append(errs, fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err))
417417
}
418418
}
419419
if *a.y.MountType == limayaml.REVSSHFS {
420420
mounts, err := a.setupMounts()
421421
if err != nil {
422-
mErr = errors.Join(mErr, err)
422+
errs = append(errs, err)
423423
}
424424
a.onClose = append(a.onClose, func() error {
425-
var unmountMErr error
425+
var unmountErrs []error
426426
for _, m := range mounts {
427427
if unmountErr := m.close(); unmountErr != nil {
428-
unmountMErr = errors.Join(unmountMErr, unmountErr)
428+
unmountErrs = append(unmountErrs, unmountErr)
429429
}
430430
}
431-
return unmountMErr
431+
return errors.Join(unmountErrs...)
432432
})
433433
}
434434
if len(a.y.AdditionalDisks) > 0 {
435435
a.onClose = append(a.onClose, func() error {
436-
var unlockMErr error
436+
var unlockErrs []error
437437
for _, d := range a.y.AdditionalDisks {
438438
disk, inspectErr := store.InspectDisk(d.Name)
439439
if inspectErr != nil {
440-
unlockMErr = errors.Join(unlockMErr, inspectErr)
440+
unlockErrs = append(unlockErrs, inspectErr)
441441
continue
442442
}
443443
logrus.Infof("Unmounting disk %q", disk.Name)
444444
if unlockErr := disk.Unlock(); unlockErr != nil {
445-
unlockMErr = errors.Join(unlockMErr, unlockErr)
445+
unlockErrs = append(unlockErrs, unlockErr)
446446
}
447447
}
448-
return unlockMErr
448+
return errors.Join(unlockErrs...)
449449
})
450450
}
451451
go a.watchGuestAgentEvents(ctx)
452452
if err := a.waitForRequirements("optional", a.optionalRequirements()); err != nil {
453-
mErr = errors.Join(mErr, err)
453+
errs = append(errs, err)
454454
}
455455
if err := a.waitForRequirements("final", a.finalRequirements()); err != nil {
456-
mErr = errors.Join(mErr, err)
456+
errs = append(errs, err)
457457
}
458458
// Copy all config files _after_ the requirements are done
459459
for _, rule := range a.y.CopyToHost {
460460
if err := copyToHost(ctx, a.sshConfig, a.sshLocalPort, rule.HostFile, rule.GuestFile); err != nil {
461-
mErr = errors.Join(mErr, err)
461+
errs = append(errs, err)
462462
}
463463
}
464-
return mErr
464+
return errors.Join(errs...)
465465
}
466466

467467
func (a *HostAgent) close() error {
468468
logrus.Infof("Shutting down the host agent")
469-
var mErr error
469+
var errs []error
470470
for i := len(a.onClose) - 1; i >= 0; i-- {
471471
f := a.onClose[i]
472472
if err := f(); err != nil {
473-
mErr = errors.Join(mErr, err)
473+
errs = append(errs, err)
474474
}
475475
}
476-
return mErr
476+
return errors.Join(errs...)
477477
}
478478

479479
func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
@@ -493,20 +493,20 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
493493

494494
a.onClose = append(a.onClose, func() error {
495495
logrus.Debugf("Stop forwarding unix sockets")
496-
var mErr error
496+
var errs []error
497497
for _, rule := range a.y.PortForwards {
498498
if rule.GuestSocket != "" {
499499
local := hostAddress(rule, guestagentapi.IPPort{})
500500
// using ctx.Background() because ctx has already been cancelled
501501
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
502-
mErr = errors.Join(mErr, err)
502+
errs = append(errs, err)
503503
}
504504
}
505505
}
506506
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
507-
mErr = errors.Join(mErr, err)
507+
errs = append(errs, err)
508508
}
509-
return mErr
509+
return errors.Join(errs...)
510510
})
511511

512512
for {

pkg/hostagent/mount.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ type mount struct {
1818
func (a *HostAgent) setupMounts() ([]*mount, error) {
1919
var (
2020
res []*mount
21-
mErr error
21+
errs []error
2222
)
2323
for _, f := range a.y.Mounts {
2424
m, err := a.setupMount(f)
2525
if err != nil {
26-
mErr = errors.Join(mErr, err)
26+
errs = append(errs, err)
2727
continue
2828
}
2929
res = append(res, m)
3030
}
31-
return res, mErr
31+
return res, errors.Join(errs...)
3232
}
3333

3434
func (a *HostAgent) setupMount(m limayaml.Mount) (*mount, error) {

pkg/hostagent/requirements.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (a *HostAgent) waitForRequirements(label string, requirements []requirement
1515
retries = 60
1616
sleepDuration = 10 * time.Second
1717
)
18-
var mErr error
18+
var errs []error
1919

2020
for i, req := range requirements {
2121
retryLoop:
@@ -28,16 +28,17 @@ func (a *HostAgent) waitForRequirements(label string, requirements []requirement
2828
}
2929
if req.fatal {
3030
logrus.Infof("No further %s requirements will be checked", label)
31-
return errors.Join(mErr, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s; skipping further checks: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
31+
errs = append(errs, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s; skipping further checks: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
32+
return errors.Join(errs...)
3233
}
3334
if j == retries-1 {
34-
mErr = errors.Join(mErr, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
35+
errs = append(errs, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
3536
break retryLoop
3637
}
3738
time.Sleep(10 * time.Second)
3839
}
3940
}
40-
return mErr
41+
return errors.Join(errs...)
4142
}
4243

4344
func (a *HostAgent) waitForRequirement(r requirement) error {

pkg/qemu/qemu_driver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ func (l *LimaQemuDriver) removeVNCFiles() error {
282282
}
283283

284284
func (l *LimaQemuDriver) killVhosts() error {
285-
var mErr error
285+
var errs []error
286286
for i, vhost := range l.vhostCmds {
287287
if err := vhost.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
288-
mErr = errors.Join(mErr, fmt.Errorf("Failed to kill virtiofsd instance #%d: %w", i, err))
288+
errs = append(errs, fmt.Errorf("Failed to kill virtiofsd instance #%d: %w", i, err))
289289
}
290290
}
291291

292-
return mErr
292+
return errors.Join(errs...)
293293
}
294294

295295
func (l *LimaQemuDriver) shutdownQEMU(ctx context.Context, timeout time.Duration, qCmd *exec.Cmd, qWaitCh <-chan error) error {

0 commit comments

Comments
 (0)