export.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. "github.com/docker/cli/cli/command"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose/v2/pkg/api"
  19. )
  20. type exportOptions struct {
  21. *ProjectOptions
  22. service string
  23. output string
  24. index int
  25. }
  26. func exportCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  27. options := exportOptions{
  28. ProjectOptions: p,
  29. }
  30. cmd := &cobra.Command{
  31. Use: "export [OPTIONS] SERVICE",
  32. Short: "Export a service container's filesystem as a tar archive",
  33. Args: cobra.MinimumNArgs(1),
  34. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  35. options.service = args[0]
  36. return nil
  37. }),
  38. RunE: Adapt(func(ctx context.Context, args []string) error {
  39. return runExport(ctx, dockerCli, backend, options)
  40. }),
  41. ValidArgsFunction: completeServiceNames(dockerCli, p),
  42. }
  43. flags := cmd.Flags()
  44. flags.IntVar(&options.index, "index", 0, "index of the container if service has multiple replicas.")
  45. flags.StringVarP(&options.output, "output", "o", "", "Write to a file, instead of STDOUT")
  46. return cmd
  47. }
  48. func runExport(ctx context.Context, dockerCli command.Cli, backend api.Service, options exportOptions) error {
  49. projectName, err := options.toProjectName(ctx, dockerCli)
  50. if err != nil {
  51. return err
  52. }
  53. exportOptions := api.ExportOptions{
  54. Service: options.service,
  55. Index: options.index,
  56. Output: options.output,
  57. }
  58. return backend.Export(ctx, projectName, exportOptions)
  59. }