Skip to content

Commit 482b622

Browse files
thaJeztahndeloof
authored andcommitted
pkg/compose: implement Export using atomicwriter
Replaces the use of cli/command.CopyToFile with an atomicwriter, as cli/command.CopyToFile will be deprecated in the next release. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent ee33143 commit 482b622

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
github.com/mitchellh/mapstructure v1.5.0
3131
github.com/moby/buildkit v0.20.1
3232
github.com/moby/patternmatcher v0.6.0
33+
github.com/moby/sys/atomicwriter v0.1.0
3334
github.com/moby/term v0.5.2
3435
github.com/morikuni/aec v1.0.0
3536
github.com/opencontainers/go-digest v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkV
330330
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
331331
github.com/moby/spdystream v0.4.0 h1:Vy79D6mHeJJjiPdFEL2yku1kl0chZpJfZcPpb16BRl8=
332332
github.com/moby/spdystream v0.4.0/go.mod h1:xBAYlnt/ay+11ShkdFKNAG7LsyK/tmNBVvVOwrfMgdI=
333+
github.com/moby/sys/atomicwriter v0.1.0 h1:kw5D/EqkBwsBFi0ss9v1VG3wIkVhzGvLklJ+w3A14Sw=
334+
github.com/moby/sys/atomicwriter v0.1.0/go.mod h1:Ul8oqv2ZMNHOceF643P6FKPXeCmYtlQMvpizfsSoaWs=
333335
github.com/moby/sys/capability v0.4.0 h1:4D4mI6KlNtWMCM1Z/K0i7RV1FkX+DBDHKVJpCndZoHk=
334336
github.com/moby/sys/capability v0.4.0/go.mod h1:4g9IK291rVkms3LKCDOoYlnV8xKwoDTpIrNEE35Wq0I=
335337
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=

pkg/compose/export.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/docker/cli/cli/command"
2626
"github.com/docker/compose/v2/pkg/api"
2727
"github.com/docker/compose/v2/pkg/progress"
28+
"github.com/moby/sys/atomicwriter"
2829
)
2930

3031
func (s *composeService) Export(ctx context.Context, projectName string, options api.ExportOptions) error {
@@ -41,11 +42,11 @@ func (s *composeService) export(ctx context.Context, projectName string, options
4142
return err
4243
}
4344

44-
if options.Output == "" && s.dockerCli.Out().IsTerminal() {
45-
return fmt.Errorf("output option is required when exporting to terminal")
46-
}
47-
48-
if err := command.ValidateOutputPath(options.Output); err != nil {
45+
if options.Output == "" {
46+
if s.dockerCli.Out().IsTerminal() {
47+
return fmt.Errorf("output option is required when exporting to terminal")
48+
}
49+
} else if err := command.ValidateOutputPath(options.Output); err != nil {
4950
return fmt.Errorf("failed to export container: %w", err)
5051
}
5152

@@ -83,9 +84,14 @@ func (s *composeService) export(ctx context.Context, projectName string, options
8384
if options.Output == "" {
8485
_, err := io.Copy(s.dockerCli.Out(), responseBody)
8586
return err
86-
}
87-
88-
if err := command.CopyToFile(options.Output, responseBody); err != nil {
87+
} else {
88+
writer, err := atomicwriter.New(options.Output, 0o600)
89+
if err != nil {
90+
return err
91+
}
92+
defer func() { _ = writer.Close() }()
93+
94+
_, err = io.Copy(writer, responseBody)
8995
return err
9096
}
9197
}

0 commit comments

Comments
 (0)