Merge pull request #5790 from thaJeztah/leftover_cleanups

cli/command: some minor cleanups
This commit is contained in:
Paweł Gronowski 2025-02-04 12:30:30 +00:00 committed by GitHub
commit dff0dc8afa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 47 additions and 49 deletions

View File

@ -48,10 +48,10 @@ func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
} }
// RunConfigCreate creates a config with the given options. // RunConfigCreate creates a config with the given options.
func RunConfigCreate(ctx context.Context, dockerCli command.Cli, options CreateOptions) error { func RunConfigCreate(ctx context.Context, dockerCLI command.Cli, options CreateOptions) error {
client := dockerCli.Client() apiClient := dockerCLI.Client()
var in io.Reader = dockerCli.In() var in io.Reader = dockerCLI.In()
if options.File != "-" { if options.File != "-" {
file, err := sequential.Open(options.File) file, err := sequential.Open(options.File)
if err != nil { if err != nil {
@ -78,11 +78,11 @@ func RunConfigCreate(ctx context.Context, dockerCli command.Cli, options CreateO
Name: options.TemplateDriver, Name: options.TemplateDriver,
} }
} }
r, err := client.ConfigCreate(ctx, spec) r, err := apiClient.ConfigCreate(ctx, spec)
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintln(dockerCli.Out(), r.ID) fmt.Fprintln(dockerCLI.Out(), r.ID)
return nil return nil
} }

View File

@ -43,15 +43,15 @@ func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command {
} }
// RunConfigInspect inspects the given Swarm config. // RunConfigInspect inspects the given Swarm config.
func RunConfigInspect(ctx context.Context, dockerCli command.Cli, opts InspectOptions) error { func RunConfigInspect(ctx context.Context, dockerCLI command.Cli, opts InspectOptions) error {
client := dockerCli.Client() apiClient := dockerCLI.Client()
if opts.Pretty { if opts.Pretty {
opts.Format = "pretty" opts.Format = "pretty"
} }
getRef := func(id string) (any, []byte, error) { getRef := func(id string) (any, []byte, error) {
return client.ConfigInspectWithRaw(ctx, id) return apiClient.ConfigInspectWithRaw(ctx, id)
} }
f := opts.Format f := opts.Format
@ -62,7 +62,7 @@ func RunConfigInspect(ctx context.Context, dockerCli command.Cli, opts InspectOp
} }
configCtx := formatter.Context{ configCtx := formatter.Context{
Output: dockerCli.Out(), Output: dockerCLI.Out(),
Format: NewFormat(f, false), Format: NewFormat(f, false),
} }

View File

@ -45,18 +45,18 @@ func newConfigListCommand(dockerCli command.Cli) *cobra.Command {
} }
// RunConfigList lists Swarm configs. // RunConfigList lists Swarm configs.
func RunConfigList(ctx context.Context, dockerCli command.Cli, options ListOptions) error { func RunConfigList(ctx context.Context, dockerCLI command.Cli, options ListOptions) error {
client := dockerCli.Client() apiClient := dockerCLI.Client()
configs, err := client.ConfigList(ctx, types.ConfigListOptions{Filters: options.Filter.Value()}) configs, err := apiClient.ConfigList(ctx, types.ConfigListOptions{Filters: options.Filter.Value()})
if err != nil { if err != nil {
return err return err
} }
format := options.Format format := options.Format
if len(format) == 0 { if len(format) == 0 {
if len(dockerCli.ConfigFile().ConfigFormat) > 0 && !options.Quiet { if len(dockerCLI.ConfigFile().ConfigFormat) > 0 && !options.Quiet {
format = dockerCli.ConfigFile().ConfigFormat format = dockerCLI.ConfigFile().ConfigFormat
} else { } else {
format = formatter.TableFormatKey format = formatter.TableFormatKey
} }
@ -67,7 +67,7 @@ func RunConfigList(ctx context.Context, dockerCli command.Cli, options ListOptio
}) })
configCtx := formatter.Context{ configCtx := formatter.Context{
Output: dockerCli.Out(), Output: dockerCLI.Out(),
Format: NewFormat(format, options.Quiet), Format: NewFormat(format, options.Quiet),
} }
return FormatWrite(configCtx, configs) return FormatWrite(configCtx, configs)

View File

@ -208,7 +208,7 @@ func resolveLocalPath(localPath string) (absPath string, err error) {
return archive.PreserveTrailingDotOrSeparator(absPath, localPath), nil 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 dstPath := copyConfig.destPath
srcPath := copyConfig.sourcePath srcPath := copyConfig.sourcePath
@ -224,11 +224,11 @@ func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cp
return err return err
} }
client := dockerCli.Client() apiClient := dockerCLI.Client()
// if client requests to follow symbol link, then must decide target file to be copied // if client requests to follow symbol link, then must decide target file to be copied
var rebaseName string var rebaseName string
if copyConfig.followLink { 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 the destination is a symbolic link, we should follow it.
if err == nil && srcStat.Mode&os.ModeSymlink != 0 { if err == nil && srcStat.Mode&os.ModeSymlink != 0 {
@ -247,14 +247,14 @@ func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cp
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel() defer cancel()
content, stat, err := client.CopyFromContainer(ctx, copyConfig.container, srcPath) content, stat, err := apiClient.CopyFromContainer(ctx, copyConfig.container, srcPath)
if err != nil { if err != nil {
return err return err
} }
defer content.Close() defer content.Close()
if dstPath == "-" { if dstPath == "-" {
_, err = io.Copy(dockerCli.Out(), content) _, err = io.Copy(dockerCLI.Out(), content)
return err return err
} }
@ -283,12 +283,12 @@ func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cp
return archive.CopyTo(preArchive, srcInfo, dstPath) 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) res := archive.CopyTo(preArchive, srcInfo, dstPath)
cancel() cancel()
<-done <-done
restore() restore()
fmt.Fprintln(dockerCli.Err(), "Successfully copied", progressHumanSize(copiedSize), "to", dstPath) _, _ = fmt.Fprintln(dockerCLI.Err(), "Successfully copied", progressHumanSize(copiedSize), "to", dstPath)
return res return res
} }
@ -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 // 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 // archive/extract API but we can use the stat info header about the
// destination to be more informed about exactly what the destination is. // 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 srcPath := copyConfig.sourcePath
dstPath := copyConfig.destPath dstPath := copyConfig.destPath
@ -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. // Prepare destination copy info by stat-ing the container path.
dstInfo := archive.CopyInfo{Path: dstPath} 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 the destination is a symbolic link, we should evaluate it.
if err == nil && dstStat.Mode&os.ModeSymlink != 0 { if err == nil && dstStat.Mode&os.ModeSymlink != 0 {
@ -324,7 +324,8 @@ func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpCo
} }
dstInfo.Path = linkTarget 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 // Validate the destination path
@ -401,16 +402,16 @@ func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpCo
} }
if copyConfig.quiet { 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) ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
restore, done := copyProgress(ctx, dockerCli.Err(), copyToContainerHeader, &copiedSize) restore, done := copyProgress(ctx, dockerCLI.Err(), copyToContainerHeader, &copiedSize)
res := client.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options) res := apiClient.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options)
cancel() cancel()
<-done <-done
restore() 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 return res
} }

View File

@ -84,13 +84,13 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command {
} }
// RunExec executes an `exec` command // RunExec executes an `exec` command
func RunExec(ctx context.Context, dockerCli command.Cli, containerIDorName string, options ExecOptions) error { func RunExec(ctx context.Context, dockerCLI command.Cli, containerIDorName string, options ExecOptions) error {
execOptions, err := parseExec(options, dockerCli.ConfigFile()) execOptions, err := parseExec(options, dockerCLI.ConfigFile())
if err != nil { if err != nil {
return err return err
} }
apiClient := dockerCli.Client() apiClient := dockerCLI.Client()
// We need to check the tty _before_ we do the ContainerExecCreate, because // 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 // otherwise if we error out we will leak execIDs on the server (and
@ -100,12 +100,12 @@ func RunExec(ctx context.Context, dockerCli command.Cli, containerIDorName strin
return err return err
} }
if !execOptions.Detach { 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 return err
} }
} }
fillConsoleSize(execOptions, dockerCli) fillConsoleSize(execOptions, dockerCLI)
response, err := apiClient.ContainerExecCreate(ctx, containerIDorName, *execOptions) response, err := apiClient.ContainerExecCreate(ctx, containerIDorName, *execOptions)
if err != nil { if err != nil {
@ -124,7 +124,7 @@ func RunExec(ctx context.Context, dockerCli command.Cli, containerIDorName strin
ConsoleSize: execOptions.ConsoleSize, ConsoleSize: execOptions.ConsoleSize,
}) })
} }
return interactiveExec(ctx, dockerCli, execOptions, execID) return interactiveExec(ctx, dockerCLI, execOptions, execID)
} }
func fillConsoleSize(execOptions *container.ExecOptions, dockerCli command.Cli) { func fillConsoleSize(execOptions *container.ExecOptions, dockerCli command.Cli) {

View File

@ -42,11 +42,9 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
return cmd return cmd
} }
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error { func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) error {
client := dockerCli.Client() apiClient := dockerCLI.Client()
return inspect.Inspect(dockerCLI.Out(), opts.refs, opts.format, func(ref string) (any, []byte, error) {
getRefFunc := func(ref string) (any, []byte, error) { return apiClient.ContainerInspectWithRaw(ctx, ref, opts.size)
return client.ContainerInspectWithRaw(ctx, ref, opts.size) })
}
return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
} }

View File

@ -39,10 +39,9 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
return cmd return cmd
} }
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error { func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) error {
client := dockerCli.Client() apiClient := dockerCLI.Client()
getRefFunc := func(ref string) (any, []byte, error) { return inspect.Inspect(dockerCLI.Out(), opts.refs, opts.format, func(ref string) (any, []byte, error) {
return client.ImageInspectWithRaw(ctx, ref) return apiClient.ImageInspectWithRaw(ctx, ref)
} })
return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
} }