(ctx context.Context, projectName string, options api.ExportOptions)
| 36 | } |
| 37 | |
| 38 | func (s *composeService) export(ctx context.Context, projectName string, options api.ExportOptions) error { |
| 39 | projectName = strings.ToLower(projectName) |
| 40 | |
| 41 | container, err := s.getSpecifiedContainer(ctx, projectName, oneOffInclude, false, options.Service, options.Index) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | |
| 46 | if options.Output == "" { |
| 47 | if s.stdout().IsTerminal() { |
| 48 | return fmt.Errorf("output option is required when exporting to terminal") |
| 49 | } |
| 50 | } else if err := command.ValidateOutputPath(options.Output); err != nil { |
| 51 | return fmt.Errorf("failed to export container: %w", err) |
| 52 | } |
| 53 | |
| 54 | name := getCanonicalContainerName(container) |
| 55 | s.events.On(api.Resource{ |
| 56 | ID: name, |
| 57 | Text: api.StatusExporting, |
| 58 | Status: api.Working, |
| 59 | }) |
| 60 | |
| 61 | responseBody, err := s.apiClient().ContainerExport(ctx, container.ID, client.ContainerExportOptions{}) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | defer func() { |
| 67 | if err := responseBody.Close(); err != nil { |
| 68 | s.events.On(errorEventf(name, "Failed to close response body: %s", err.Error())) |
| 69 | } |
| 70 | }() |
| 71 | |
| 72 | if !s.dryRun { |
| 73 | if options.Output == "" { |
| 74 | _, err := io.Copy(s.stdout(), responseBody) |
| 75 | return err |
| 76 | } else { |
| 77 | writer, err := atomicwriter.New(options.Output, 0o600) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | defer func() { _ = writer.Close() }() |
| 82 | |
| 83 | _, err = io.Copy(writer, responseBody) |
| 84 | return err |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | s.events.On(api.Resource{ |
| 89 | ID: name, |
| 90 | Text: api.StatusExported, |
| 91 | Status: api.Done, |
| 92 | }) |
| 93 | |
| 94 | return nil |
| 95 | } |
no test coverage detected