Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli/command: deprecate CopyToFile and reimplement with atomicwriter #5914

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions cli/command/container/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/docker/pkg/atomicwriter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -42,26 +43,30 @@ func NewExportCommand(dockerCli command.Cli) *cobra.Command {
}

func runExport(ctx context.Context, dockerCli command.Cli, opts exportOptions) error {
if opts.output == "" && dockerCli.Out().IsTerminal() {
return errors.New("cowardly refusing to save to a terminal. Use the -o flag or redirect")
}

if err := command.ValidateOutputPath(opts.output); err != nil {
return errors.Wrap(err, "failed to export container")
var output io.Writer
if opts.output == "" {
if dockerCli.Out().IsTerminal() {
return errors.New("cowardly refusing to save to a terminal. Use the -o flag or redirect")
}
output = dockerCli.Out()
} else {
if err := command.ValidateOutputPath(opts.output); err != nil {
return errors.Wrap(err, "failed to export container")
}
writer, err := atomicwriter.New(opts.output, 0o600)
if err != nil {
return err
}
defer writer.Close()
output = writer
}

clnt := dockerCli.Client()

responseBody, err := clnt.ContainerExport(ctx, opts.container)
responseBody, err := dockerCli.Client().ContainerExport(ctx, opts.container)
if err != nil {
return err
}
defer responseBody.Close()

if opts.output == "" {
_, err := io.Copy(dockerCli.Out(), responseBody)
return err
}

return command.CopyToFile(opts.output, responseBody)
_, err = io.Copy(output, responseBody)
return err
}
35 changes: 21 additions & 14 deletions cli/command/image/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/atomicwriter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -49,14 +50,6 @@ func NewSaveCommand(dockerCli command.Cli) *cobra.Command {

// RunSave performs a save against the engine based on the specified options
func RunSave(ctx context.Context, dockerCli command.Cli, opts saveOptions) error {
if opts.output == "" && dockerCli.Out().IsTerminal() {
return errors.New("cowardly refusing to save to a terminal. Use the -o flag or redirect")
}

if err := command.ValidateOutputPath(opts.output); err != nil {
return errors.Wrap(err, "failed to save image")
}

var options []client.ImageSaveOption
if opts.platform != "" {
p, err := platforms.Parse(opts.platform)
Expand All @@ -67,16 +60,30 @@ func RunSave(ctx context.Context, dockerCli command.Cli, opts saveOptions) error
options = append(options, client.ImageSaveWithPlatforms(p))
}

var output io.Writer
if opts.output == "" {
if dockerCli.Out().IsTerminal() {
return errors.New("cowardly refusing to save to a terminal. Use the -o flag or redirect")
}
output = dockerCli.Out()
} else {
if err := command.ValidateOutputPath(opts.output); err != nil {
return errors.Wrap(err, "failed to save image")
}
writer, err := atomicwriter.New(opts.output, 0o600)
if err != nil {
return err
}
defer writer.Close()
output = writer
}

responseBody, err := dockerCli.Client().ImageSave(ctx, opts.images, options...)
if err != nil {
return err
}
defer responseBody.Close()

if opts.output == "" {
_, err := io.Copy(dockerCli.Out(), responseBody)
return err
}

return command.CopyToFile(opts.output, responseBody)
_, err = io.Copy(output, responseBody)
return err
}
28 changes: 7 additions & 21 deletions cli/command/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,23 @@ import (
"github.com/docker/cli/cli/streams"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/errdefs"
"github.com/moby/sys/sequential"
"github.com/docker/docker/pkg/atomicwriter"
"github.com/moby/term"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)

// CopyToFile writes the content of the reader to the specified file
//
// Deprecated: use [atomicwriter.New].
func CopyToFile(outfile string, r io.Reader) error {
// We use sequential file access here to avoid depleting the standby list
// on Windows. On Linux, this is a call directly to os.CreateTemp
tmpFile, err := sequential.CreateTemp(filepath.Dir(outfile), ".docker_temp_")
if err != nil {
return err
}

tmpPath := tmpFile.Name()

_, err = io.Copy(tmpFile, r)
tmpFile.Close()

writer, err := atomicwriter.New(outfile, 0o600)
if err != nil {
os.Remove(tmpPath)
return err
}

if err = os.Rename(tmpPath, outfile); err != nil {
os.Remove(tmpPath)
return err
}

return nil
defer writer.Close()
_, err = io.Copy(writer, r)
return err
}

var ErrPromptTerminated = errdefs.Cancelled(errors.New("prompt terminated"))
Expand Down
Loading