Skip to content

Commit e81d76f

Browse files
committed
cli/command/container: minor cleanups
- 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 <github@gone.nl>
1 parent deaa601 commit e81d76f

3 files changed

Lines changed: 27 additions & 28 deletions

File tree

cli/command/container/cp.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func resolveLocalPath(localPath string) (absPath string, err error) {
208208
return archive.PreserveTrailingDotOrSeparator(absPath, localPath), nil
209209
}
210210

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

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

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

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

250-
content, stat, err := client.CopyFromContainer(ctx, copyConfig.container, srcPath)
250+
content, stat, err := apiClient.CopyFromContainer(ctx, copyConfig.container, srcPath)
251251
if err != nil {
252252
return err
253253
}
254254
defer content.Close()
255255

256256
if dstPath == "-" {
257-
_, err = io.Copy(dockerCli.Out(), content)
257+
_, err = io.Copy(dockerCLI.Out(), content)
258258
return err
259259
}
260260

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

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

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

@@ -309,10 +309,10 @@ func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpCo
309309
}
310310
}
311311

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

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

326326
dstInfo.Path = linkTarget
327-
dstStat, err = client.ContainerStatPath(ctx, copyConfig.container, linkTarget)
327+
dstStat, err = apiClient.ContainerStatPath(ctx, copyConfig.container, linkTarget)
328+
// FIXME(thaJeztah): unhandled error (should this return?)
328329
}
329330

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

403404
if copyConfig.quiet {
404-
return client.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options)
405+
return apiClient.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options)
405406
}
406407

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

415416
return res
416417
}

cli/command/container/exec.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command {
8484
}
8585

8686
// RunExec executes an `exec` command
87-
func RunExec(ctx context.Context, dockerCli command.Cli, containerIDorName string, options ExecOptions) error {
88-
execOptions, err := parseExec(options, dockerCli.ConfigFile())
87+
func RunExec(ctx context.Context, dockerCLI command.Cli, containerIDorName string, options ExecOptions) error {
88+
execOptions, err := parseExec(options, dockerCLI.ConfigFile())
8989
if err != nil {
9090
return err
9191
}
9292

93-
apiClient := dockerCli.Client()
93+
apiClient := dockerCLI.Client()
9494

9595
// We need to check the tty _before_ we do the ContainerExecCreate, because
9696
// 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
100100
return err
101101
}
102102
if !execOptions.Detach {
103-
if err := dockerCli.In().CheckTty(execOptions.AttachStdin, execOptions.Tty); err != nil {
103+
if err := dockerCLI.In().CheckTty(execOptions.AttachStdin, execOptions.Tty); err != nil {
104104
return err
105105
}
106106
}
107107

108-
fillConsoleSize(execOptions, dockerCli)
108+
fillConsoleSize(execOptions, dockerCLI)
109109

110110
response, err := apiClient.ContainerExecCreate(ctx, containerIDorName, *execOptions)
111111
if err != nil {
@@ -124,7 +124,7 @@ func RunExec(ctx context.Context, dockerCli command.Cli, containerIDorName strin
124124
ConsoleSize: execOptions.ConsoleSize,
125125
})
126126
}
127-
return interactiveExec(ctx, dockerCli, execOptions, execID)
127+
return interactiveExec(ctx, dockerCLI, execOptions, execID)
128128
}
129129

130130
func fillConsoleSize(execOptions *container.ExecOptions, dockerCli command.Cli) {

cli/command/container/inspect.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
4242
return cmd
4343
}
4444

45-
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
46-
client := dockerCli.Client()
47-
48-
getRefFunc := func(ref string) (any, []byte, error) {
49-
return client.ContainerInspectWithRaw(ctx, ref, opts.size)
50-
}
51-
return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
45+
func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) error {
46+
apiClient := dockerCLI.Client()
47+
return inspect.Inspect(dockerCLI.Out(), opts.refs, opts.format, func(ref string) (any, []byte, error) {
48+
return apiClient.ContainerInspectWithRaw(ctx, ref, opts.size)
49+
})
5250
}

0 commit comments

Comments
 (0)