Slight cleanup/refactor of attachContainer
Return early in case there's an error, and declare variables closer
to where they're used.
@@ -214,14 +214,20 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
return nil
}
-func attachContainer(
- ctx context.Context,
- dockerCli command.Cli,
- errCh *chan error,
- config *container.Config,
- containerID string,
-) (func(), error) {
- stdout, stderr := dockerCli.Out(), dockerCli.Err()
+func attachContainer(ctx context.Context, dockerCli command.Cli, errCh *chan error, config *container.Config, containerID string) (func(), error) {
+ options := types.ContainerAttachOptions{
+ Stream: true,
+ Stdin: config.AttachStdin,
+ Stdout: config.AttachStdout,
+ Stderr: config.AttachStderr,
+ DetachKeys: dockerCli.ConfigFile().DetachKeys,
+ }
+
+ resp, errAttach := dockerCli.Client().ContainerAttach(ctx, containerID, options)
+ if errAttach != nil {
+ return nil, errAttach
+ }
+
var (
out, cerr io.Writer
in io.ReadCloser
@@ -230,29 +236,16 @@ func attachContainer(
in = dockerCli.In()
}
if config.AttachStdout {
- out = stdout
+ out = dockerCli.Out()
}
if config.AttachStderr {
if config.Tty {
- cerr = stdout
+ cerr = dockerCli.Out()
} else {
- cerr = stderr
+ cerr = dockerCli.Err()
}
}
- options := types.ContainerAttachOptions{
- Stream: true,
- Stdin: config.AttachStdin,
- Stdout: config.AttachStdout,
- Stderr: config.AttachStderr,
- DetachKeys: dockerCli.ConfigFile().DetachKeys,
- }
-
- resp, errAttach := dockerCli.Client().ContainerAttach(ctx, containerID, options)
- if errAttach != nil {
- return nil, errAttach
- }
-
ch := make(chan error, 1)
*errCh = ch
@@ -284,7 +277,7 @@ func reportError(stderr io.Writer, name string, str string, withHelp bool) {
if withHelp {
str += "\nSee 'docker " + name + " --help'."
}
- fmt.Fprintln(stderr, "docker:", str)
+ _, _ = fmt.Fprintln(stderr, "docker:", str)
}
// if container start fails with 'not found'/'no such' error, return 127
