Skip to content

Commit 947bf75

Browse files
authored
Merge pull request #1228 from junnplus/multiple-ids
fix found multiple IDs when container walker
2 parents 33fb8e8 + 2087827 commit 947bf75

File tree

15 files changed

+36
-12
lines changed

15 files changed

+36
-12
lines changed

cmd/nerdctl/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func commitAction(cmd *cobra.Command, args []string) error {
6262
Client: client,
6363
OnFound: func(ctx context.Context, found containerwalker.Found) error {
6464
if found.MatchCount > 1 {
65-
return fmt.Errorf("ambiguous ID %q", found.Req)
65+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
6666
}
6767
imageID, err := commit.Commit(ctx, client, found.Container, opts)
6868
if err != nil {

cmd/nerdctl/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func execAction(cmd *cobra.Command, args []string) error {
8282
Client: client,
8383
OnFound: func(ctx context.Context, found containerwalker.Found) error {
8484
if found.MatchCount > 1 {
85-
return fmt.Errorf("ambiguous ID %q", found.Req)
85+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
8686
}
8787
return execActionWithContainer(ctx, cmd, args, found.Container, client)
8888
},

cmd/nerdctl/history.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func historyAction(cmd *cobra.Command, args []string) error {
8383
walker := &imagewalker.ImageWalker{
8484
Client: client,
8585
OnFound: func(ctx context.Context, found imagewalker.Found) error {
86+
if found.MatchCount > 1 {
87+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
88+
}
8689
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
8790
defer cancel()
8891
img := containerd.NewImage(client, found.Image)
@@ -147,8 +150,6 @@ func historyAction(cmd *cobra.Command, args []string) error {
147150
errs = append(errs, err)
148151
} else if n == 0 {
149152
errs = append(errs, fmt.Errorf("no such object: %s", req))
150-
} else if n > 1 {
151-
return fmt.Errorf("multiple IDs found with provided prefix: %s", req)
152153
}
153154
}
154155
if len(errs) > 0 {

cmd/nerdctl/kill.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ func killAction(cmd *cobra.Command, args []string) error {
7070
walker := &containerwalker.ContainerWalker{
7171
Client: client,
7272
OnFound: func(ctx context.Context, found containerwalker.Found) error {
73+
if found.MatchCount > 1 {
74+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
75+
}
7376
if err := killContainer(ctx, found.Container, signal); err != nil {
7477
if errdefs.IsNotFound(err) {
75-
fmt.Fprintf(cmd.ErrOrStderr(), "Error response from daemon: Cannot kill container: %s: No such container: %s\n", found.Req, found.Req)
78+
fmt.Fprintf(cmd.ErrOrStderr(), "No such container: %s\n", found.Req)
7679
os.Exit(1)
7780
}
7881
return err

cmd/nerdctl/logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func logsAction(cmd *cobra.Command, args []string) error {
100100
Client: client,
101101
OnFound: func(ctx context.Context, found containerwalker.Found) error {
102102
if found.MatchCount > 1 {
103-
return fmt.Errorf("ambiguous ID %q", found.Req)
103+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
104104
}
105105
l, err := found.Container.Labels(ctx)
106106
if err != nil {

cmd/nerdctl/pause.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func pauseAction(cmd *cobra.Command, args []string) error {
5050
walker := &containerwalker.ContainerWalker{
5151
Client: client,
5252
OnFound: func(ctx context.Context, found containerwalker.Found) error {
53+
if found.MatchCount > 1 {
54+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
55+
}
5356
if err := pauseContainer(ctx, client, found.Container.ID()); err != nil {
5457
return err
5558
}

cmd/nerdctl/port.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func portAction(cmd *cobra.Command, args []string) error {
8383
Client: client,
8484
OnFound: func(ctx context.Context, found containerwalker.Found) error {
8585
if found.MatchCount > 1 {
86-
return fmt.Errorf("ambiguous ID %q", found.Req)
86+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
8787
}
8888
return printPort(ctx, cmd, found.Container, argPort, argProto)
8989
},

cmd/nerdctl/rename.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func renameAction(cmd *cobra.Command, args []string) error {
6868
Client: client,
6969
OnFound: func(ctx context.Context, found containerwalker.Found) error {
7070
if found.MatchCount > 1 {
71-
return fmt.Errorf("ambiguous ID %q", found.Req)
71+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
7272
}
7373
return renameContainer(ctx, found.Container, args[1], ns, namest, hostst)
7474
},

cmd/nerdctl/rm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func rmAction(cmd *cobra.Command, args []string) error {
7979
walker := &containerwalker.ContainerWalker{
8080
Client: client,
8181
OnFound: func(ctx context.Context, found containerwalker.Found) error {
82+
if found.MatchCount > 1 {
83+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
84+
}
8285
err = removeContainer(cmd, ctx, found.Container, ns, force, removeAnonVolumes)
8386
if err != nil {
8487
return err

cmd/nerdctl/start.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ func startAction(cmd *cobra.Command, args []string) error {
6969
walker := &containerwalker.ContainerWalker{
7070
Client: client,
7171
OnFound: func(ctx context.Context, found containerwalker.Found) error {
72+
if found.MatchCount > 1 {
73+
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
74+
}
7275
if err := startContainer(ctx, found.Container, flagA); err != nil {
7376
return err
7477
}

0 commit comments

Comments
 (0)