|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + |
| 23 | + "github.com/docker/cli/cli" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + |
| 26 | + "github.com/docker/compose-cli/api/compose" |
| 27 | +) |
| 28 | + |
| 29 | +type copyOptions struct { |
| 30 | + *projectOptions |
| 31 | + |
| 32 | + source string |
| 33 | + destination string |
| 34 | + index int |
| 35 | + all bool |
| 36 | + followLink bool |
| 37 | + copyUIDGID bool |
| 38 | +} |
| 39 | + |
| 40 | +func copyCommand(p *projectOptions, backend compose.Service) *cobra.Command { |
| 41 | + opts := copyOptions{ |
| 42 | + projectOptions: p, |
| 43 | + } |
| 44 | + copyCmd := &cobra.Command{ |
| 45 | + Use: `cp [OPTIONS] SERVICE:SRC_PATH DEST_PATH|- |
| 46 | + docker compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH`, |
| 47 | + Short: "Copy files/folders between a service container and the local filesystem", |
| 48 | + Args: cli.ExactArgs(2), |
| 49 | + RunE: Adapt(func(ctx context.Context, args []string) error { |
| 50 | + if args[0] == "" { |
| 51 | + return errors.New("source can not be empty") |
| 52 | + } |
| 53 | + if args[1] == "" { |
| 54 | + return errors.New("destination can not be empty") |
| 55 | + } |
| 56 | + |
| 57 | + opts.source = args[0] |
| 58 | + opts.destination = args[1] |
| 59 | + return runCopy(ctx, backend, opts) |
| 60 | + }), |
| 61 | + } |
| 62 | + |
| 63 | + flags := copyCmd.Flags() |
| 64 | + flags.IntVar(&opts.index, "index", 1, "Index of the container if there are multiple instances of a service [default: 1].") |
| 65 | + flags.BoolVar(&opts.all, "all", false, "Copy to all the containers of the service.") |
| 66 | + flags.BoolVarP(&opts.followLink, "follow-link", "L", false, "Always follow symbol link in SRC_PATH") |
| 67 | + flags.BoolVarP(&opts.copyUIDGID, "archive", "a", false, "Archive mode (copy all uid/gid information)") |
| 68 | + |
| 69 | + return copyCmd |
| 70 | +} |
| 71 | + |
| 72 | +func runCopy(ctx context.Context, backend compose.Service, opts copyOptions) error { |
| 73 | + projects, err := opts.toProject(nil) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + return backend.Copy(ctx, projects, compose.CopyOptions{ |
| 79 | + Source: opts.source, |
| 80 | + Destination: opts.destination, |
| 81 | + All: opts.all, |
| 82 | + Index: opts.index, |
| 83 | + FollowLink: opts.followLink, |
| 84 | + CopyUIDGID: opts.copyUIDGID, |
| 85 | + }) |
| 86 | +} |
0 commit comments