cp.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/spf13/cobra"
  19. "github.com/docker/compose/v2/pkg/api"
  20. )
  21. type copyOptions struct {
  22. *projectOptions
  23. source string
  24. destination string
  25. index int
  26. all bool
  27. followLink bool
  28. copyUIDGID bool
  29. }
  30. func copyCommand(p *projectOptions, backend api.Service) *cobra.Command {
  31. opts := copyOptions{
  32. projectOptions: p,
  33. }
  34. copyCmd := &cobra.Command{
  35. Use: `cp [OPTIONS] SERVICE:SRC_PATH DEST_PATH|-
  36. docker compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH`,
  37. Short: "Copy files/folders between a service container and the local filesystem",
  38. Args: cli.ExactArgs(2),
  39. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  40. if args[0] == "" {
  41. return errors.New("source can not be empty")
  42. }
  43. if args[1] == "" {
  44. return errors.New("destination can not be empty")
  45. }
  46. return nil
  47. }),
  48. RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
  49. opts.source = args[0]
  50. opts.destination = args[1]
  51. return runCopy(ctx, backend, opts)
  52. }),
  53. ValidArgsFunction: serviceCompletion(p),
  54. }
  55. flags := copyCmd.Flags()
  56. flags.IntVar(&opts.index, "index", 0, "Index of the container if there are multiple instances of a service .")
  57. flags.BoolVar(&opts.all, "all", false, "Copy to all the containers of the service.")
  58. flags.MarkHidden("all") //nolint:errcheck
  59. flags.MarkDeprecated("all", "By default all the containers of the service will get the source file/directory to be copied.") //nolint:errcheck
  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, backend api.Service, opts copyOptions) error {
  65. name, err := opts.toProjectName()
  66. if err != nil {
  67. return err
  68. }
  69. return backend.Copy(ctx, name, api.CopyOptions{
  70. Source: opts.source,
  71. Destination: opts.destination,
  72. All: opts.all,
  73. Index: opts.index,
  74. FollowLink: opts.followLink,
  75. CopyUIDGID: opts.copyUIDGID,
  76. })
  77. }