Skip to content

Commit 7a89e89

Browse files
committed
cli/command/container: waitExitOrRemoved: take APIClient as argument
It only needs the API client, not the whole DockerCLI. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 0b7c72c commit 7a89e89

4 files changed

Lines changed: 14 additions & 15 deletions

File tree

cli/command/container/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
187187
defer closeFn()
188188
}
189189

190-
statusChan := waitExitOrRemoved(ctx, dockerCli, containerID, copts.autoRemove)
190+
statusChan := waitExitOrRemoved(ctx, dockerCli.Client(), containerID, copts.autoRemove)
191191

192192
// start the container
193193
if err := client.ContainerStart(ctx, containerID, types.ContainerStartOptions{}); err != nil {

cli/command/container/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func RunStart(dockerCli command.Cli, opts *StartOptions) error {
145145

146146
// 3. We should open a channel for receiving status code of the container
147147
// no matter it's detached, removed on daemon side(--rm) or exit normally.
148-
statusChan := waitExitOrRemoved(ctx, dockerCli, c.ID, c.HostConfig.AutoRemove)
148+
statusChan := waitExitOrRemoved(ctx, dockerCli.Client(), c.ID, c.HostConfig.AutoRemove)
149149

150150
// 4. Start the container.
151151
err = dockerCli.Client().ContainerStart(ctx, c.ID, types.ContainerStartOptions{

cli/command/container/utils.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import (
44
"context"
55
"strconv"
66

7-
"github.com/docker/cli/cli/command"
87
"github.com/docker/docker/api/types"
98
"github.com/docker/docker/api/types/container"
109
"github.com/docker/docker/api/types/events"
1110
"github.com/docker/docker/api/types/filters"
1211
"github.com/docker/docker/api/types/versions"
12+
"github.com/docker/docker/client"
1313
"github.com/sirupsen/logrus"
1414
)
1515

16-
func waitExitOrRemoved(ctx context.Context, dockerCli command.Cli, containerID string, waitRemove bool) <-chan int {
16+
func waitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containerID string, waitRemove bool) <-chan int {
1717
if len(containerID) == 0 {
1818
// containerID can never be empty
1919
panic("Internal Error: waitExitOrRemoved needs a containerID as parameter")
@@ -22,16 +22,16 @@ func waitExitOrRemoved(ctx context.Context, dockerCli command.Cli, containerID s
2222
// Older versions used the Events API, and even older versions did not
2323
// support server-side removal. This legacyWaitExitOrRemoved method
2424
// preserves that old behavior and any issues it may have.
25-
if versions.LessThan(dockerCli.Client().ClientVersion(), "1.30") {
26-
return legacyWaitExitOrRemoved(ctx, dockerCli, containerID, waitRemove)
25+
if versions.LessThan(apiClient.ClientVersion(), "1.30") {
26+
return legacyWaitExitOrRemoved(ctx, apiClient, containerID, waitRemove)
2727
}
2828

2929
condition := container.WaitConditionNextExit
3030
if waitRemove {
3131
condition = container.WaitConditionRemoved
3232
}
3333

34-
resultC, errC := dockerCli.Client().ContainerWait(ctx, containerID, condition)
34+
resultC, errC := apiClient.ContainerWait(ctx, containerID, condition)
3535

3636
statusC := make(chan int)
3737
go func() {
@@ -52,7 +52,7 @@ func waitExitOrRemoved(ctx context.Context, dockerCli command.Cli, containerID s
5252
return statusC
5353
}
5454

55-
func legacyWaitExitOrRemoved(ctx context.Context, dockerCli command.Cli, containerID string, waitRemove bool) <-chan int {
55+
func legacyWaitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containerID string, waitRemove bool) <-chan int {
5656
var removeErr error
5757
statusChan := make(chan int)
5858
exitCode := 125
@@ -65,7 +65,7 @@ func legacyWaitExitOrRemoved(ctx context.Context, dockerCli command.Cli, contain
6565
Filters: f,
6666
}
6767
eventCtx, cancel := context.WithCancel(ctx)
68-
eventq, errq := dockerCli.Client().Events(eventCtx, options)
68+
eventq, errq := apiClient.Events(eventCtx, options)
6969

7070
eventProcessor := func(e events.Message) bool {
7171
stopProcessing := false
@@ -84,9 +84,9 @@ func legacyWaitExitOrRemoved(ctx context.Context, dockerCli command.Cli, contain
8484
} else {
8585
// If we are talking to an older daemon, `AutoRemove` is not supported.
8686
// We need to fall back to the old behavior, which is client-side removal
87-
if versions.LessThan(dockerCli.Client().ClientVersion(), "1.25") {
87+
if versions.LessThan(apiClient.ClientVersion(), "1.25") {
8888
go func() {
89-
removeErr = dockerCli.Client().ContainerRemove(ctx, containerID, types.ContainerRemoveOptions{RemoveVolumes: true})
89+
removeErr = apiClient.ContainerRemove(ctx, containerID, types.ContainerRemoveOptions{RemoveVolumes: true})
9090
if removeErr != nil {
9191
logrus.Errorf("error removing container: %v", removeErr)
9292
cancel() // cancel the event Q

cli/command/container/utils_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package container
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67
"testing"
78

8-
"github.com/docker/cli/internal/test"
99
"github.com/docker/docker/api"
1010
"github.com/docker/docker/api/types/container"
11-
"github.com/pkg/errors"
1211
"gotest.tools/v3/assert"
1312
is "gotest.tools/v3/assert/cmp"
1413
)
@@ -24,7 +23,7 @@ func waitFn(cid string) (<-chan container.WaitResponse, <-chan error) {
2423
res.StatusCode = 42
2524
resC <- res
2625
case strings.Contains(cid, "non-existent"):
27-
err := errors.Errorf("no such container: %v", cid)
26+
err := fmt.Errorf("no such container: %v", cid)
2827
errC <- err
2928
case strings.Contains(cid, "wait-error"):
3029
res.Error = &container.WaitExitError{Message: "removal failed"}
@@ -61,7 +60,7 @@ func TestWaitExitOrRemoved(t *testing.T) {
6160
},
6261
}
6362

64-
client := test.NewFakeCli(&fakeClient{waitFunc: waitFn, Version: api.DefaultVersion})
63+
client := &fakeClient{waitFunc: waitFn, Version: api.DefaultVersion}
6564
for _, testcase := range testcases {
6665
statusC := waitExitOrRemoved(context.Background(), client, testcase.cid, true)
6766
exitCode := <-statusC

0 commit comments

Comments
 (0)