Skip to content

Commit a2ec50a

Browse files
committed
make container an explicit, required parameter
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent a4abe42 commit a2ec50a

3 files changed

Lines changed: 18 additions & 20 deletions

File tree

cli/command/container/attach.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ type AttachOptions struct {
2222
NoStdin bool
2323
Proxy bool
2424
DetachKeys string
25-
26-
Container string
2725
}
2826

2927
func inspectContainerAndCheckState(ctx context.Context, cli client.APIClient, args string) (*types.ContainerJSON, error) {
@@ -47,14 +45,15 @@ func inspectContainerAndCheckState(ctx context.Context, cli client.APIClient, ar
4745
// NewAttachCommand creates a new cobra.Command for `docker attach`
4846
func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
4947
var opts AttachOptions
48+
var container string
5049

5150
cmd := &cobra.Command{
5251
Use: "attach [OPTIONS] CONTAINER",
5352
Short: "Attach local standard input, output, and error streams to a running container",
5453
Args: cli.ExactArgs(1),
5554
RunE: func(cmd *cobra.Command, args []string) error {
56-
opts.Container = args[0]
57-
return RunAttach(context.Background(), dockerCli, &opts)
55+
container = args[0]
56+
return RunAttach(context.Background(), dockerCli, container, &opts)
5857
},
5958
Annotations: map[string]string{
6059
"aliases": "docker container attach, docker attach",
@@ -72,13 +71,13 @@ func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
7271
}
7372

7473
// RunAttach executes an `attach` command
75-
func RunAttach(ctx context.Context, dockerCli command.Cli, opts *AttachOptions) error {
74+
func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts *AttachOptions) error {
7675
apiClient := dockerCli.Client()
7776

7877
// request channel to wait for client
79-
resultC, errC := apiClient.ContainerWait(ctx, opts.Container, "")
78+
resultC, errC := apiClient.ContainerWait(ctx, target, "")
8079

81-
c, err := inspectContainerAndCheckState(ctx, apiClient, opts.Container)
80+
c, err := inspectContainerAndCheckState(ctx, apiClient, target)
8281
if err != nil {
8382
return err
8483
}
@@ -107,11 +106,11 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, opts *AttachOptions)
107106

108107
if opts.Proxy && !c.Config.Tty {
109108
sigc := notifyAllSignals()
110-
go ForwardAllSignals(ctx, dockerCli, opts.Container, sigc)
109+
go ForwardAllSignals(ctx, dockerCli, target, sigc)
111110
defer signal.StopCatch(sigc)
112111
}
113112

114-
resp, errAttach := apiClient.ContainerAttach(ctx, opts.Container, options)
113+
resp, errAttach := apiClient.ContainerAttach(ctx, target, options)
115114
if errAttach != nil {
116115
return errAttach
117116
}
@@ -125,13 +124,13 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, opts *AttachOptions)
125124
// the container and not exit.
126125
//
127126
// Recheck the container's state to avoid attach block.
128-
_, err = inspectContainerAndCheckState(ctx, apiClient, opts.Container)
127+
_, err = inspectContainerAndCheckState(ctx, apiClient, target)
129128
if err != nil {
130129
return err
131130
}
132131

133132
if c.Config.Tty && dockerCli.Out().IsTerminal() {
134-
resizeTTY(ctx, dockerCli, opts.Container)
133+
resizeTTY(ctx, dockerCli, target)
135134
}
136135

137136
streamer := hijackedIOStreamer{

cli/command/container/exec.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type ExecOptions struct {
2828
Privileged bool
2929
Env opts.ListOpts
3030
Workdir string
31-
Container string
3231
Command []string
3332
EnvFile opts.ListOpts
3433
}
@@ -44,15 +43,16 @@ func NewExecOptions() ExecOptions {
4443
// NewExecCommand creates a new cobra.Command for `docker exec`
4544
func NewExecCommand(dockerCli command.Cli) *cobra.Command {
4645
options := NewExecOptions()
46+
var container string
4747

4848
cmd := &cobra.Command{
4949
Use: "exec [OPTIONS] CONTAINER COMMAND [ARG...]",
5050
Short: "Execute a command in a running container",
5151
Args: cli.RequiresMinArgs(2),
5252
RunE: func(cmd *cobra.Command, args []string) error {
53-
options.Container = args[0]
53+
container = args[0]
5454
options.Command = args[1:]
55-
return RunExec(context.Background(), dockerCli, options)
55+
return RunExec(context.Background(), dockerCli, container, options)
5656
},
5757
ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(container types.Container) bool {
5858
return container.State != "paused"
@@ -96,7 +96,7 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command {
9696
}
9797

9898
// RunExec executes an `exec` command
99-
func RunExec(ctx context.Context, dockerCli command.Cli, options ExecOptions) error {
99+
func RunExec(ctx context.Context, dockerCli command.Cli, container string, options ExecOptions) error {
100100
execConfig, err := parseExec(options, dockerCli.ConfigFile())
101101
if err != nil {
102102
return err
@@ -108,7 +108,7 @@ func RunExec(ctx context.Context, dockerCli command.Cli, options ExecOptions) er
108108
// otherwise if we error out we will leak execIDs on the server (and
109109
// there's no easy way to clean those up). But also in order to make "not
110110
// exist" errors take precedence we do a dummy inspect first.
111-
if _, err := client.ContainerInspect(ctx, options.Container); err != nil {
111+
if _, err := client.ContainerInspect(ctx, container); err != nil {
112112
return err
113113
}
114114
if !execConfig.Detach {
@@ -119,7 +119,7 @@ func RunExec(ctx context.Context, dockerCli command.Cli, options ExecOptions) er
119119

120120
fillConsoleSize(execConfig, dockerCli)
121121

122-
response, err := client.ContainerExecCreate(ctx, options.Container, *execConfig)
122+
response, err := client.ContainerExecCreate(ctx, container, *execConfig)
123123
if err != nil {
124124
return err
125125
}

cli/command/container/exec_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ func TestRunExec(t *testing.T) {
169169
{
170170
doc: "successful detach",
171171
options: withDefaultOpts(ExecOptions{
172-
Container: "thecontainer",
173-
Detach: true,
172+
Detach: true,
174173
}),
175174
client: fakeClient{execCreateFunc: execCreateWithID},
176175
},
@@ -195,7 +194,7 @@ func TestRunExec(t *testing.T) {
195194
t.Run(testcase.doc, func(t *testing.T) {
196195
cli := test.NewFakeCli(&testcase.client)
197196

198-
err := RunExec(context.Background(), cli, testcase.options)
197+
err := RunExec(context.Background(), cli, "thecontainer", testcase.options)
199198
if testcase.expectedError != "" {
200199
assert.ErrorContains(t, err, testcase.expectedError)
201200
} else {

0 commit comments

Comments
 (0)