Skip to content

Commit 4f85512

Browse files
committed
make runAttach public and allow passing context
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 9cb175f commit 4f85512

2 files changed

Lines changed: 33 additions & 25 deletions

File tree

cli/command/container/attach.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import (
1717
"github.com/spf13/cobra"
1818
)
1919

20-
type attachOptions struct {
21-
noStdin bool
22-
proxy bool
23-
detachKeys string
20+
// AttachOptions group options for `attach` command
21+
type AttachOptions struct {
22+
NoStdin bool
23+
Proxy bool
24+
DetachKeys string
2425

25-
container string
26+
Container string
2627
}
2728

2829
func inspectContainerAndCheckState(ctx context.Context, cli client.APIClient, args string) (*types.ContainerJSON, error) {
@@ -45,15 +46,15 @@ func inspectContainerAndCheckState(ctx context.Context, cli client.APIClient, ar
4546

4647
// NewAttachCommand creates a new cobra.Command for `docker attach`
4748
func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
48-
var opts attachOptions
49+
var opts AttachOptions
4950

5051
cmd := &cobra.Command{
5152
Use: "attach [OPTIONS] CONTAINER",
5253
Short: "Attach local standard input, output, and error streams to a running container",
5354
Args: cli.ExactArgs(1),
5455
RunE: func(cmd *cobra.Command, args []string) error {
55-
opts.container = args[0]
56-
return runAttach(dockerCli, &opts)
56+
opts.Container = args[0]
57+
return RunAttach(dockerCli, &opts)
5758
},
5859
Annotations: map[string]string{
5960
"aliases": "docker container attach, docker attach",
@@ -64,36 +65,39 @@ func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
6465
}
6566

6667
flags := cmd.Flags()
67-
flags.BoolVar(&opts.noStdin, "no-stdin", false, "Do not attach STDIN")
68-
flags.BoolVar(&opts.proxy, "sig-proxy", true, "Proxy all received signals to the process")
69-
flags.StringVar(&opts.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
68+
flags.BoolVar(&opts.NoStdin, "no-stdin", false, "Do not attach STDIN")
69+
flags.BoolVar(&opts.Proxy, "sig-proxy", true, "Proxy all received signals to the process")
70+
flags.StringVar(&opts.DetachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
7071
return cmd
7172
}
7273

73-
func runAttach(dockerCli command.Cli, opts *attachOptions) error {
74-
ctx := context.Background()
74+
func RunAttach(dockerCli command.Cli, opts *AttachOptions) error {
75+
return RunAttachWithContext(context.Background(), dockerCli, opts)
76+
}
77+
78+
func RunAttachWithContext(ctx context.Context, dockerCli command.Cli, opts *AttachOptions) error {
7579
apiClient := dockerCli.Client()
7680

7781
// request channel to wait for client
78-
resultC, errC := apiClient.ContainerWait(ctx, opts.container, "")
82+
resultC, errC := apiClient.ContainerWait(ctx, opts.Container, "")
7983

80-
c, err := inspectContainerAndCheckState(ctx, apiClient, opts.container)
84+
c, err := inspectContainerAndCheckState(ctx, apiClient, opts.Container)
8185
if err != nil {
8286
return err
8387
}
8488

85-
if err := dockerCli.In().CheckTty(!opts.noStdin, c.Config.Tty); err != nil {
89+
if err := dockerCli.In().CheckTty(!opts.NoStdin, c.Config.Tty); err != nil {
8690
return err
8791
}
8892

8993
detachKeys := dockerCli.ConfigFile().DetachKeys
90-
if opts.detachKeys != "" {
91-
detachKeys = opts.detachKeys
94+
if opts.DetachKeys != "" {
95+
detachKeys = opts.DetachKeys
9296
}
9397

9498
options := container.AttachOptions{
9599
Stream: true,
96-
Stdin: !opts.noStdin && c.Config.OpenStdin,
100+
Stdin: !opts.NoStdin && c.Config.OpenStdin,
97101
Stdout: true,
98102
Stderr: true,
99103
DetachKeys: detachKeys,
@@ -104,13 +108,13 @@ func runAttach(dockerCli command.Cli, opts *attachOptions) error {
104108
in = dockerCli.In()
105109
}
106110

107-
if opts.proxy && !c.Config.Tty {
111+
if opts.Proxy && !c.Config.Tty {
108112
sigc := notifyAllSignals()
109-
go ForwardAllSignals(ctx, dockerCli, opts.container, sigc)
113+
go ForwardAllSignals(ctx, dockerCli, opts.Container, sigc)
110114
defer signal.StopCatch(sigc)
111115
}
112116

113-
resp, errAttach := apiClient.ContainerAttach(ctx, opts.container, options)
117+
resp, errAttach := apiClient.ContainerAttach(ctx, opts.Container, options)
114118
if errAttach != nil {
115119
return errAttach
116120
}
@@ -124,13 +128,13 @@ func runAttach(dockerCli command.Cli, opts *attachOptions) error {
124128
// the container and not exit.
125129
//
126130
// Recheck the container's state to avoid attach block.
127-
_, err = inspectContainerAndCheckState(ctx, apiClient, opts.container)
131+
_, err = inspectContainerAndCheckState(ctx, apiClient, opts.Container)
128132
if err != nil {
129133
return err
130134
}
131135

132136
if c.Config.Tty && dockerCli.Out().IsTerminal() {
133-
resizeTTY(ctx, dockerCli, opts.container)
137+
resizeTTY(ctx, dockerCli, opts.Container)
134138
}
135139

136140
streamer := hijackedIOStreamer{

cli/command/container/exec.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,16 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command {
9797

9898
// RunExec executes an `exec` command
9999
func RunExec(dockerCli command.Cli, options ExecOptions) error {
100+
return RunExecWithContext(context.Background(), dockerCli, options)
101+
}
102+
103+
// RunExecWithContext executes an `exec` command
104+
func RunExecWithContext(ctx context.Context, dockerCli command.Cli, options ExecOptions) error {
100105
execConfig, err := parseExec(options, dockerCli.ConfigFile())
101106
if err != nil {
102107
return err
103108
}
104109

105-
ctx := context.Background()
106110
client := dockerCli.Client()
107111

108112
// We need to check the tty _before_ we do the ContainerExecCreate, because

0 commit comments

Comments
 (0)