export.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "strings"
  19. "github.com/docker/cli/cli/command"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/docker/compose/v2/pkg/progress"
  22. "github.com/moby/sys/atomicwriter"
  23. )
  24. func (s *composeService) Export(ctx context.Context, projectName string, options api.ExportOptions) error {
  25. return progress.RunWithTitle(ctx, func(ctx context.Context) error {
  26. return s.export(ctx, projectName, options)
  27. }, s.stdinfo(), "Exporting")
  28. }
  29. func (s *composeService) export(ctx context.Context, projectName string, options api.ExportOptions) error {
  30. projectName = strings.ToLower(projectName)
  31. container, err := s.getSpecifiedContainer(ctx, projectName, oneOffInclude, false, options.Service, options.Index)
  32. if err != nil {
  33. return err
  34. }
  35. if options.Output == "" {
  36. if s.dockerCli.Out().IsTerminal() {
  37. return fmt.Errorf("output option is required when exporting to terminal")
  38. }
  39. } else if err := command.ValidateOutputPath(options.Output); err != nil {
  40. return fmt.Errorf("failed to export container: %w", err)
  41. }
  42. clnt := s.dockerCli.Client()
  43. w := progress.ContextWriter(ctx)
  44. name := getCanonicalContainerName(container)
  45. msg := fmt.Sprintf("export %s to %s", name, options.Output)
  46. w.Event(progress.Event{
  47. ID: name,
  48. Text: msg,
  49. Status: progress.Working,
  50. StatusText: "Exporting",
  51. })
  52. responseBody, err := clnt.ContainerExport(ctx, container.ID)
  53. if err != nil {
  54. return err
  55. }
  56. defer func() {
  57. if err := responseBody.Close(); err != nil {
  58. w.Event(progress.Event{
  59. ID: name,
  60. Text: msg,
  61. Status: progress.Error,
  62. StatusText: fmt.Sprintf("Failed to close response body: %v", err),
  63. })
  64. }
  65. }()
  66. if !s.dryRun {
  67. if options.Output == "" {
  68. _, err := io.Copy(s.dockerCli.Out(), responseBody)
  69. return err
  70. } else {
  71. writer, err := atomicwriter.New(options.Output, 0o600)
  72. if err != nil {
  73. return err
  74. }
  75. defer func() { _ = writer.Close() }()
  76. _, err = io.Copy(writer, responseBody)
  77. return err
  78. }
  79. }
  80. w.Event(progress.Event{
  81. ID: name,
  82. Text: msg,
  83. Status: progress.Done,
  84. StatusText: "Exported",
  85. })
  86. return nil
  87. }