cli/command/container: minor cleanup in attach

- rename confusing `target` argument, and use `containerID` in all places;
  also make the variable more clearly local-scoped.
- rename `dockerCli` to be correctly camel-case, and to be consistent in
  all places in this file.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-01-26 13:38:09 +01:00
parent a1bd689a4d
commit 690f63e6d3
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 11 additions and 12 deletions

View File

@ -43,22 +43,21 @@ func inspectContainerAndCheckState(ctx context.Context, apiClient client.APIClie
} }
// NewAttachCommand creates a new cobra.Command for `docker attach` // NewAttachCommand creates a new cobra.Command for `docker attach`
func NewAttachCommand(dockerCli command.Cli) *cobra.Command { func NewAttachCommand(dockerCLI command.Cli) *cobra.Command {
var opts AttachOptions var opts AttachOptions
var ctr string
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "attach [OPTIONS] CONTAINER", Use: "attach [OPTIONS] CONTAINER",
Short: "Attach local standard input, output, and error streams to a running container", Short: "Attach local standard input, output, and error streams to a running container",
Args: cli.ExactArgs(1), Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
ctr = args[0] containerID := args[0]
return RunAttach(cmd.Context(), dockerCli, ctr, &opts) return RunAttach(cmd.Context(), dockerCLI, containerID, &opts)
}, },
Annotations: map[string]string{ Annotations: map[string]string{
"aliases": "docker container attach, docker attach", "aliases": "docker container attach, docker attach",
}, },
ValidArgsFunction: completion.ContainerNames(dockerCli, false, func(ctr types.Container) bool { ValidArgsFunction: completion.ContainerNames(dockerCLI, false, func(ctr types.Container) bool {
return ctr.State != "paused" return ctr.State != "paused"
}), }),
} }
@ -71,13 +70,13 @@ func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
} }
// RunAttach executes an `attach` command // RunAttach executes an `attach` command
func RunAttach(ctx context.Context, dockerCLI command.Cli, target string, opts *AttachOptions) error { func RunAttach(ctx context.Context, dockerCLI command.Cli, containerID string, opts *AttachOptions) error {
apiClient := dockerCLI.Client() apiClient := dockerCLI.Client()
// request channel to wait for client // request channel to wait for client
resultC, errC := apiClient.ContainerWait(ctx, target, "") resultC, errC := apiClient.ContainerWait(ctx, containerID, "")
c, err := inspectContainerAndCheckState(ctx, apiClient, target) c, err := inspectContainerAndCheckState(ctx, apiClient, containerID)
if err != nil { if err != nil {
return err return err
} }
@ -106,11 +105,11 @@ func RunAttach(ctx context.Context, dockerCLI command.Cli, target string, opts *
if opts.Proxy && !c.Config.Tty { if opts.Proxy && !c.Config.Tty {
sigc := notifyAllSignals() sigc := notifyAllSignals()
go ForwardAllSignals(ctx, apiClient, target, sigc) go ForwardAllSignals(ctx, apiClient, containerID, sigc)
defer signal.StopCatch(sigc) defer signal.StopCatch(sigc)
} }
resp, errAttach := apiClient.ContainerAttach(ctx, target, options) resp, errAttach := apiClient.ContainerAttach(ctx, containerID, options)
if errAttach != nil { if errAttach != nil {
return errAttach return errAttach
} }
@ -124,13 +123,13 @@ func RunAttach(ctx context.Context, dockerCLI command.Cli, target string, opts *
// the container and not exit. // the container and not exit.
// //
// Recheck the container's state to avoid attach block. // Recheck the container's state to avoid attach block.
_, err = inspectContainerAndCheckState(ctx, apiClient, target) _, err = inspectContainerAndCheckState(ctx, apiClient, containerID)
if err != nil { if err != nil {
return err return err
} }
if c.Config.Tty && dockerCLI.Out().IsTerminal() { if c.Config.Tty && dockerCLI.Out().IsTerminal() {
resizeTTY(ctx, dockerCLI, target) resizeTTY(ctx, dockerCLI, containerID)
} }
streamer := hijackedIOStreamer{ streamer := hijackedIOStreamer{