cp: Make gocyclo happy

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2023-03-30 01:04:37 +00:00
parent 90b7bc36d4
commit b9a1b0928a
1 changed files with 19 additions and 22 deletions

View File

@ -237,15 +237,13 @@ func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cp
RebaseName: rebaseName, RebaseName: rebaseName,
} }
stderrIsTerm := streams.NewOut(dockerCli.Err()).IsTerminal()
var copiedSize float64 var copiedSize float64
if !copyConfig.quiet { if !copyConfig.quiet {
content = &copyProgressPrinter{ content = &copyProgressPrinter{
ReadCloser: content, ReadCloser: content,
writer: dockerCli.Err(), writer: dockerCli.Err(),
total: &copiedSize, total: &copiedSize,
isTerm: stderrIsTerm, isTerm: streams.NewOut(dockerCli.Err()).IsTerminal(),
header: copyFromContainerHeader, header: copyFromContainerHeader,
} }
} }
@ -260,15 +258,9 @@ func copyFromContainer(ctx context.Context, dockerCli command.Cli, copyConfig cp
return archive.CopyTo(preArchive, srcInfo, dstPath) return archive.CopyTo(preArchive, srcInfo, dstPath)
} }
if stderrIsTerm { restore := prepareTTYCopyProgress(dockerCli)
fmt.Fprint(dockerCli.Err(), aec.Save)
fmt.Fprintln(dockerCli.Err(), "Preparing to copy...")
}
res := archive.CopyTo(preArchive, srcInfo, dstPath) res := archive.CopyTo(preArchive, srcInfo, dstPath)
if stderrIsTerm { restore()
fmt.Fprint(dockerCli.Err(), aec.Restore)
fmt.Fprint(dockerCli.Err(), aec.EraseLine(aec.EraseModes.All))
}
fmt.Fprintln(dockerCli.Err(), "Successfully copied", units.HumanSize(copiedSize), "to", dstPath) fmt.Fprintln(dockerCli.Err(), "Successfully copied", units.HumanSize(copiedSize), "to", dstPath)
return res return res
@ -329,8 +321,6 @@ func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpCo
copiedSize float64 copiedSize float64
) )
stderrIsTerm := streams.NewOut(dockerCli.Err()).IsTerminal()
if srcPath == "-" { if srcPath == "-" {
content = os.Stdin content = os.Stdin
resolvedDstPath = dstInfo.Path resolvedDstPath = dstInfo.Path
@ -375,7 +365,7 @@ func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpCo
ReadCloser: content, ReadCloser: content,
writer: dockerCli.Err(), writer: dockerCli.Err(),
total: &copiedSize, total: &copiedSize,
isTerm: stderrIsTerm, isTerm: streams.NewOut(dockerCli.Err()).IsTerminal(),
header: copyToContainerHeader, header: copyToContainerHeader,
} }
} }
@ -390,20 +380,27 @@ func copyToContainer(ctx context.Context, dockerCli command.Cli, copyConfig cpCo
return client.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options) return client.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options)
} }
if stderrIsTerm { restore := prepareTTYCopyProgress(dockerCli)
fmt.Fprint(dockerCli.Err(), aec.Save)
fmt.Fprintln(dockerCli.Err(), "Preparing to copy...")
}
res := client.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options) res := client.CopyToContainer(ctx, copyConfig.container, resolvedDstPath, content, options)
if stderrIsTerm { restore()
fmt.Fprint(dockerCli.Err(), aec.Restore)
fmt.Fprint(dockerCli.Err(), aec.EraseLine(aec.EraseModes.All))
}
fmt.Fprintln(dockerCli.Err(), "Successfully copied", units.HumanSize(copiedSize), "to", copyConfig.container+":"+dstInfo.Path) fmt.Fprintln(dockerCli.Err(), "Successfully copied", units.HumanSize(copiedSize), "to", copyConfig.container+":"+dstInfo.Path)
return res return res
} }
func prepareTTYCopyProgress(cli command.Cli) func() {
if !streams.NewOut(cli.Err()).IsTerminal() {
return func() {}
}
fmt.Fprint(cli.Err(), aec.Save)
fmt.Fprintln(cli.Err(), "Preparing to copy...")
return func() {
fmt.Fprint(cli.Err(), aec.Restore)
fmt.Fprint(cli.Err(), aec.EraseLine(aec.EraseModes.All))
}
}
// We use `:` as a delimiter between CONTAINER and PATH, but `:` could also be // We use `:` as a delimiter between CONTAINER and PATH, but `:` could also be
// in a valid LOCALPATH, like `file:name.txt`. We can resolve this ambiguity by // in a valid LOCALPATH, like `file:name.txt`. We can resolve this ambiguity by
// requiring a LOCALPATH with a `:` to be made explicit with a relative or // requiring a LOCALPATH with a `:` to be made explicit with a relative or