Skip to content

Commit a1ed02a

Browse files
authored
Merge pull request #1971 from djdongjin/unify-multi-error
Optimize id walkers and muti error handling
2 parents d41705c + ff5f333 commit a1ed02a

File tree

20 files changed

+145
-217
lines changed

20 files changed

+145
-217
lines changed

cmd/nerdctl/container_stats.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,8 @@ func statsAction(cmd *cobra.Command, args []string) error {
277277
},
278278
}
279279

280-
for _, req := range args {
281-
n, err := walker.Walk(ctx, req)
282-
if err != nil {
283-
return err
284-
} else if n == 0 {
285-
return fmt.Errorf("no such container %s", req)
286-
}
280+
if err := walker.WalkAll(ctx, args, false); err != nil {
281+
return err
287282
}
288283

289284
// make sure each container get at least one valid stat data

cmd/nerdctl/container_update.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,8 @@ func updateAction(cmd *cobra.Command, args []string) error {
110110
return err
111111
},
112112
}
113-
for _, req := range args {
114-
n, err := walker.Walk(ctx, req)
115-
if err != nil {
116-
return err
117-
} else if n == 0 {
118-
return fmt.Errorf("no such container %s", req)
119-
}
120-
}
121-
return nil
113+
114+
return walker.WalkAll(ctx, args, true)
122115
}
123116

124117
func getUpdateOption(cmd *cobra.Command, globalOptions types.GlobalCommandOptions) (updateResourceOptions, error) {

cmd/nerdctl/image_history.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,7 @@ func historyAction(cmd *cobra.Command, args []string) error {
143143
},
144144
}
145145

146-
var errs []error
147-
for _, req := range args {
148-
n, err := walker.Walk(ctx, req)
149-
if err != nil {
150-
errs = append(errs, err)
151-
} else if n == 0 {
152-
errs = append(errs, fmt.Errorf("no such object: %s", req))
153-
}
154-
}
155-
if len(errs) > 0 {
156-
return fmt.Errorf("%d errors: %v", len(errs), errs)
157-
}
158-
return nil
146+
return walker.WalkAll(ctx, args, true)
159147
}
160148

161149
type historyPrinter struct {

pkg/cmd/container/inspect.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/containerd/nerdctl/pkg/formatter"
2828
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
2929
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
30+
"github.com/sirupsen/logrus"
3031
)
3132

3233
// Inspect prints detailed information for each container in `containers`.
@@ -40,20 +41,13 @@ func Inspect(ctx context.Context, client *containerd.Client, containers []string
4041
OnFound: f.Handler,
4142
}
4243

43-
var errs []error
44-
for _, req := range containers {
45-
n, err := walker.Walk(ctx, req)
46-
if err != nil {
47-
errs = append(errs, err)
48-
} else if n == 0 {
49-
errs = append(errs, fmt.Errorf("no such container: %s", req))
44+
err := walker.WalkAll(ctx, containers, true)
45+
if len(f.entries) > 0 {
46+
if formatErr := formatter.FormatSlice(options.Format, options.Stdout, f.entries); formatErr != nil {
47+
logrus.Error(formatErr)
5048
}
5149
}
52-
if len(errs) > 0 {
53-
return fmt.Errorf("%d errors: %v", len(errs), errs)
54-
}
55-
56-
return formatter.FormatSlice(options.Format, options.Stdout, f.entries)
50+
return err
5751
}
5852

5953
type containerInspector struct {

pkg/cmd/container/kill.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,8 @@ func Kill(ctx context.Context, client *containerd.Client, reqs []string, options
6060
return err
6161
},
6262
}
63-
for _, req := range reqs {
64-
n, err := walker.Walk(ctx, req)
65-
if err != nil {
66-
return err
67-
} else if n == 0 {
68-
return fmt.Errorf("no such container %s", req)
69-
}
70-
}
71-
return nil
63+
64+
return walker.WalkAll(ctx, reqs, true)
7265
}
7366

7467
func killContainer(ctx context.Context, container containerd.Container, signal syscall.Signal) error {

pkg/cmd/container/pause.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ package container
1818

1919
import (
2020
"context"
21-
"errors"
2221
"fmt"
23-
"strings"
2422

2523
"github.com/containerd/containerd"
2624
"github.com/containerd/nerdctl/pkg/api/types"
@@ -45,18 +43,5 @@ func Pause(ctx context.Context, client *containerd.Client, reqs []string, option
4543
},
4644
}
4745

48-
var errs []string
49-
for _, req := range reqs {
50-
n, err := walker.Walk(ctx, req)
51-
if err != nil {
52-
errs = append(errs, err.Error())
53-
} else if n == 0 {
54-
errs = append(errs, fmt.Sprintf("no such container %s", req))
55-
}
56-
}
57-
58-
if len(errs) > 0 {
59-
return errors.New(strings.Join(errs, "\n"))
60-
}
61-
return nil
46+
return walker.WalkAll(ctx, reqs, true)
6247
}

pkg/cmd/container/remove.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,13 @@ func Remove(ctx context.Context, client *containerd.Client, containers []string,
7777
return err
7878
},
7979
}
80-
for _, req := range containers {
81-
n, err := walker.Walk(ctx, req)
82-
if err == nil && n == 0 {
83-
err = fmt.Errorf("no such container %s", req)
84-
}
85-
if err != nil {
86-
if options.Force {
87-
logrus.Error(err)
88-
} else {
89-
return err
90-
}
91-
}
80+
81+
err := walker.WalkAll(ctx, containers, true)
82+
if err != nil && options.Force {
83+
logrus.Error(err)
84+
return nil
9285
}
93-
return nil
86+
return err
9487
}
9588

9689
// RemoveContainer removes a container from containerd store.

pkg/cmd/container/restart.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ func Restart(ctx context.Context, client *containerd.Client, containers []string
4444
return err
4545
},
4646
}
47-
for _, req := range containers {
48-
n, err := walker.Walk(ctx, req)
49-
if err != nil {
50-
return err
51-
} else if n == 0 {
52-
return fmt.Errorf("no such container %s", req)
53-
}
54-
}
5547

56-
return nil
48+
return walker.WalkAll(ctx, containers, true)
5749
}

pkg/cmd/container/start.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,5 @@ func Start(ctx context.Context, client *containerd.Client, reqs []string, option
5252
},
5353
}
5454

55-
for _, req := range reqs {
56-
n, err := walker.Walk(ctx, req)
57-
if err != nil {
58-
return err
59-
} else if n == 0 {
60-
return fmt.Errorf("no such container %s", req)
61-
}
62-
}
63-
return nil
55+
return walker.WalkAll(ctx, reqs, true)
6456
}

pkg/cmd/container/stop.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
2828
)
2929

30+
// Stop stops a list of containers specified by `reqs`.
3031
func Stop(ctx context.Context, client *containerd.Client, reqs []string, opt types.ContainerStopOptions) error {
3132
walker := &containerwalker.ContainerWalker{
3233
Client: client,
@@ -45,13 +46,6 @@ func Stop(ctx context.Context, client *containerd.Client, reqs []string, opt typ
4546
return err
4647
},
4748
}
48-
for _, req := range reqs {
49-
n, err := walker.Walk(ctx, req)
50-
if err != nil {
51-
return err
52-
} else if n == 0 {
53-
return fmt.Errorf("no such container %s", req)
54-
}
55-
}
56-
return nil
49+
50+
return walker.WalkAll(ctx, reqs, true)
5751
}

0 commit comments

Comments
 (0)