cp.go 2.6 KB

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