cp.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "errors"
  17. "github.com/docker/cli/cli"
  18. "github.com/docker/cli/cli/command"
  19. "github.com/docker/compose/v2/pkg/compose"
  20. "github.com/spf13/cobra"
  21. "github.com/docker/compose/v2/pkg/api"
  22. )
  23. type copyOptions struct {
  24. *ProjectOptions
  25. source string
  26. destination string
  27. index int
  28. all bool
  29. followLink bool
  30. copyUIDGID bool
  31. }
  32. func copyCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  33. opts := copyOptions{
  34. ProjectOptions: p,
  35. }
  36. copyCmd := &cobra.Command{
  37. Use: `cp [OPTIONS] SERVICE:SRC_PATH DEST_PATH|-
  38. docker compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH`,
  39. Short: "Copy files/folders between a service container and the local filesystem",
  40. Args: cli.ExactArgs(2),
  41. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  42. if args[0] == "" {
  43. return errors.New("source can not be empty")
  44. }
  45. if args[1] == "" {
  46. return errors.New("destination can not be empty")
  47. }
  48. return nil
  49. }),
  50. RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
  51. opts.source = args[0]
  52. opts.destination = args[1]
  53. return runCopy(ctx, dockerCli, backendOptions, opts)
  54. }),
  55. ValidArgsFunction: completeServiceNames(dockerCli, p),
  56. }
  57. flags := copyCmd.Flags()
  58. flags.IntVar(&opts.index, "index", 0, "Index of the container if service has multiple replicas")
  59. flags.BoolVar(&opts.all, "all", false, "Include containers created by the run command")
  60. flags.BoolVarP(&opts.followLink, "follow-link", "L", false, "Always follow symbol link in SRC_PATH")
  61. flags.BoolVarP(&opts.copyUIDGID, "archive", "a", false, "Archive mode (copy all uid/gid information)")
  62. return copyCmd
  63. }
  64. func runCopy(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts copyOptions) error {
  65. name, err := opts.toProjectName(ctx, dockerCli)
  66. if err != nil {
  67. return err
  68. }
  69. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  70. if err != nil {
  71. return err
  72. }
  73. return backend.Copy(ctx, name, api.CopyOptions{
  74. Source: opts.source,
  75. Destination: opts.destination,
  76. All: opts.all,
  77. Index: opts.index,
  78. FollowLink: opts.followLink,
  79. CopyUIDGID: opts.copyUIDGID,
  80. })
  81. }