Skip to content

Commit

Permalink
cli/command/container: minor cleanups
Browse files Browse the repository at this point in the history
- use apiClient instead of client for the API client to
  prevent shadowing imports.
- use dockerCLI with Go's standard camelCase casing.
- suppress some errors to make my IDE and linters happier

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Feb 3, 2025
1 parent deaa601 commit e81d76f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
31 changes: 16 additions & 15 deletions cli/command/container/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func resolveLocalPath(localPath string) (absPath string, err error) {
return archive.PreserveTrailingDotOrSeparator(absPath, localPath), nil
}

func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpConfig) (err error) {
func copyFromContainer(ctx context.Context, dockerCLI command.Cli, copyConfig cpConfig) (err error) {
dstPath := copyConfig.destPath
srcPath := copyConfig.sourcePath

Expand All @@ -224,11 +224,11 @@ func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cp
return err
}

client := dockerCli.Client()
apiClient := dockerCLI.Client()
// if client requests to follow symbol link, then must decide target file to be copied
var rebaseName string
if copyConfig.followLink {
srcStat, err := client.ContainerStatPath(ctx, copyConfig.container, srcPath)
srcStat, err := apiClient.ContainerStatPath(ctx, copyConfig.container, srcPath)

// If the destination is a symbolic link, we should follow it.
if err == nil && srcStat.Mode&os.ModeSymlink != 0 {
Expand All @@ -247,14 +247,14 @@ func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cp
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()

content, stat, err := client.CopyFromContainer(ctx, copyConfig.container, srcPath)
content, stat, err := apiClient.CopyFromContainer(ctx, copyConfig.container, srcPath)
if err != nil {
return err
}
defer content.Close()

if dstPath == "-" {
_, err = io.Copy(dockerCli.Out(), content)
_, err = io.Copy(dockerCLI.Out(), content)
return err
}

Expand Down Expand Up @@ -283,12 +283,12 @@ func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cp
return archive.CopyTo(preArchive, srcInfo, dstPath)
}

restore, done := copyProgress(ctx, dockerCli.Err(), copyFromContainerHeader, &copiedSize)
restore, done := copyProgress(ctx, dockerCLI.Err(), copyFromContainerHeader, &copiedSize)
res := archive.CopyTo(preArchive, srcInfo, dstPath)
cancel()
<-done
restore()
fmt.Fprintln(dockerCli.Err(), "Successfully copied", progressHumanSize(copiedSize), "to", dstPath)
_, _ = fmt.Fprintln(dockerCLI.Err(), "Successfully copied", progressHumanSize(copiedSize), "to", dstPath)

return res
}
Expand All @@ -297,7 +297,7 @@ func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cp
// about both the source and destination. The API is a simple tar
// archive/extract API but we can use the stat info header about the
// destination to be more informed about exactly what the destination is.
func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpConfig) (err error) {
func copyToContainer(ctx context.Context, dockerCLI command.Cli, copyConfig cpConfig) (err error) {
srcPath := copyConfig.sourcePath
dstPath := copyConfig.destPath

Expand All @@ -309,10 +309,10 @@ func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpCo
}
}

client := dockerCli.Client()
apiClient := dockerCLI.Client()
// Prepare destination copy info by stat-ing the container path.
dstInfo := archive.CopyInfo{Path: dstPath}
dstStat, err := client.ContainerStatPath(ctx, copyConfig.container, dstPath)
dstStat, err := apiClient.ContainerStatPath(ctx, copyConfig.container, dstPath)

// If the destination is a symbolic link, we should evaluate it.
if err == nil && dstStat.Mode&os.ModeSymlink != 0 {
Expand All @@ -324,7 +324,8 @@ func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpCo
}

dstInfo.Path = linkTarget
dstStat, err = client.ContainerStatPath(ctx, copyConfig.container, linkTarget)
dstStat, err = apiClient.ContainerStatPath(ctx, copyConfig.container, linkTarget)
// FIXME(thaJeztah): unhandled error (should this return?)
}

// Validate the destination path
Expand Down Expand Up @@ -401,16 +402,16 @@ func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpCo
}

if copyConfig.quiet {
return client.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options)
return apiClient.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options)
}

ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
restore, done := copyProgress(ctx, dockerCli.Err(), copyToContainerHeader, &copiedSize)
res := client.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options)
restore, done := copyProgress(ctx, dockerCLI.Err(), copyToContainerHeader, &copiedSize)
res := apiClient.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options)
cancel()
<-done
restore()
fmt.Fprintln(dockerCli.Err(), "Successfully copied", progressHumanSize(copiedSize), "to", copyConfig.container+":"+dstInfo.Path)
fmt.Fprintln(dockerCLI.Err(), "Successfully copied", progressHumanSize(copiedSize), "to", copyConfig.container+":"+dstInfo.Path)

return res
}
Expand Down
12 changes: 6 additions & 6 deletions cli/command/container/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command {
}

// RunExec executes an `exec` command
func RunExec(ctx context.Context, dockerCli command.Cli, containerIDorName string, options ExecOptions) error {
execOptions, err := parseExec(options, dockerCli.ConfigFile())
func RunExec(ctx context.Context, dockerCLI command.Cli, containerIDorName string, options ExecOptions) error {
execOptions, err := parseExec(options, dockerCLI.ConfigFile())
if err != nil {
return err
}

apiClient := dockerCli.Client()
apiClient := dockerCLI.Client()

// We need to check the tty _before_ we do the ContainerExecCreate, because
// otherwise if we error out we will leak execIDs on the server (and
Expand All @@ -100,12 +100,12 @@ func RunExec(ctx context.Context, dockerCli command.Cli, containerIDorName strin
return err
}
if !execOptions.Detach {
if err := dockerCli.In().CheckTty(execOptions.AttachStdin, execOptions.Tty); err != nil {
if err := dockerCLI.In().CheckTty(execOptions.AttachStdin, execOptions.Tty); err != nil {
return err
}
}

fillConsoleSize(execOptions, dockerCli)
fillConsoleSize(execOptions, dockerCLI)

response, err := apiClient.ContainerExecCreate(ctx, containerIDorName, *execOptions)
if err != nil {
Expand All @@ -124,7 +124,7 @@ func RunExec(ctx context.Context, dockerCli command.Cli, containerIDorName strin
ConsoleSize: execOptions.ConsoleSize,
})
}
return interactiveExec(ctx, dockerCli, execOptions, execID)
return interactiveExec(ctx, dockerCLI, execOptions, execID)
}

func fillConsoleSize(execOptions *container.ExecOptions, dockerCli command.Cli) {
Expand Down
12 changes: 5 additions & 7 deletions cli/command/container/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
client := dockerCli.Client()

getRefFunc := func(ref string) (any, []byte, error) {
return client.ContainerInspectWithRaw(ctx, ref, opts.size)
}
return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) error {
apiClient := dockerCLI.Client()
return inspect.Inspect(dockerCLI.Out(), opts.refs, opts.format, func(ref string) (any, []byte, error) {
return apiClient.ContainerInspectWithRaw(ctx, ref, opts.size)
})
}

0 comments on commit e81d76f

Please sign in to comment.