Skip to content

Commit af78247

Browse files
committed
WIP: refactor RunAttach to receive container.AttachOptions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 248585e commit af78247

1 file changed

Lines changed: 32 additions & 26 deletions

File tree

cli/command/container/attach.go

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,22 @@ func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
5151
Short: "Attach local standard input, output, and error streams to a running container",
5252
Args: cli.ExactArgs(1),
5353
RunE: func(cmd *cobra.Command, args []string) error {
54+
attachStdIn := true
55+
if opts.NoStdin {
56+
// TODO(thaJeztah): this is the tricky one: can we use container.AttachOptions for this one without it being ambiguous?
57+
attachStdIn = false
58+
}
59+
5460
containerID := args[0]
61+
disableSignalProxy := !opts.Proxy
5562
ctx := context.Background()
56-
return RunAttach(ctx, dockerCli, containerID, &opts)
63+
return RunAttach(ctx, dockerCli, containerID, disableSignalProxy, container.AttachOptions{
64+
Stream: true,
65+
Stdin: attachStdIn,
66+
Stdout: true,
67+
Stderr: true,
68+
DetachKeys: opts.DetachKeys,
69+
})
5770
},
5871
Annotations: map[string]string{
5972
"aliases": "docker container attach, docker attach",
@@ -65,61 +78,54 @@ func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
6578

6679
flags := cmd.Flags()
6780
flags.BoolVar(&opts.NoStdin, "no-stdin", false, "Do not attach STDIN")
81+
// Is this feature still used?
82+
// It was added in https://github.com/moby/moby/commit/4918769b1ac2d38f23087b766140e6a7f8979310 to allow forwarding signals to containers
83+
// Changed in https://github.com/moby/moby/commit/333bc23f21e8423d3085632db99a6d1df227c5f1
84+
// And changed to be enabled by default in https://github.com/moby/moby/commit/e0b59ab52b87b8fc15dd5534c3231fdd74843f9f (unless a TTY is attached)
85+
// related: https://github.com/moby/moby/commit/e0b59ab52b87b8fc15dd5534c3231fdd74843f9f#commitcomment-25897874
86+
// related: https://github.com/moby/moby/issues/9098
87+
// related: https://github.com/docker/cli/pull/1841
6888
flags.BoolVar(&opts.Proxy, "sig-proxy", true, "Proxy all received signals to the process")
6989
flags.StringVar(&opts.DetachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
7090
return cmd
7191
}
7292

73-
// RunAttach executes an `attach` command
74-
func RunAttach(ctx context.Context, dockerCli command.Cli, containerID string, opts *AttachOptions) error {
93+
// RunAttach attaches to the given container.
94+
func RunAttach(ctx context.Context, dockerCli command.Cli, containerID string, disableSignalProxy bool, opts container.AttachOptions) error {
7595
apiClient := dockerCli.Client()
7696

77-
attachStdIn := true
78-
if opts.NoStdin {
79-
// TODO(thaJeztah): this is the tricky one: can we use container.AttachOptions for this one without it being ambiguous?
80-
attachStdIn = false
81-
}
82-
8397
c, err := inspectContainerAndCheckState(ctx, apiClient, containerID)
8498
if err != nil {
8599
return err
86100
}
87101

88-
if attachStdIn {
89-
if err := dockerCli.In().CheckTty(attachStdIn, c.Config.Tty); err != nil {
102+
if opts.Stdin {
103+
if err := dockerCli.In().CheckTty(opts.Stdin, c.Config.Tty); err != nil {
90104
return err
91105
}
92106
if !c.Config.OpenStdin {
93107
// TODO(thaJeztah): should this produce an error?
94-
attachStdIn = false
108+
opts.Stdin = false
95109
}
96110
}
97111

98-
detachKeys := opts.DetachKeys
99112
if opts.DetachKeys == "" {
100-
detachKeys = dockerCli.ConfigFile().DetachKeys
101-
}
102-
103-
options := container.AttachOptions{
104-
Stream: true,
105-
Stdin: attachStdIn,
106-
Stdout: true,
107-
Stderr: true,
108-
DetachKeys: detachKeys,
113+
opts.DetachKeys = dockerCli.ConfigFile().DetachKeys
109114
}
110115

111116
var in io.ReadCloser
112-
if options.Stdin {
117+
if opts.Stdin {
113118
in = dockerCli.In()
114119
}
115120

116-
if opts.Proxy && !c.Config.Tty {
121+
// TODO(thaJeztah): should this still depend on Config.Tty? It's unconditionally enabled on `docker exec` since https://github.com/docker/cli/pull/1841/files
122+
if !disableSignalProxy && !c.Config.Tty {
117123
sigc := notifyAllSignals()
118124
go ForwardAllSignals(ctx, apiClient, containerID, sigc)
119125
defer signal.StopCatch(sigc)
120126
}
121127

122-
resp, errAttach := apiClient.ContainerAttach(ctx, containerID, options)
128+
resp, errAttach := apiClient.ContainerAttach(ctx, containerID, opts)
123129
if errAttach != nil {
124130
return errAttach
125131
}
@@ -149,7 +155,7 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, containerID string, o
149155
errorStream: dockerCli.Err(),
150156
resp: resp,
151157
tty: c.Config.Tty,
152-
detachKeys: options.DetachKeys,
158+
detachKeys: opts.DetachKeys,
153159
}
154160

155161
if err := streamer.stream(ctx); err != nil {

0 commit comments

Comments
 (0)