github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_cp.go (about) 1 /* 2 Copyright The containerd 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 main 18 19 import ( 20 "errors" 21 22 "github.com/containerd/nerdctl/pkg/clientutil" 23 "github.com/containerd/nerdctl/pkg/cmd/compose" 24 "github.com/containerd/nerdctl/pkg/composer" 25 "github.com/containerd/nerdctl/pkg/rootlessutil" 26 "github.com/spf13/cobra" 27 ) 28 29 func newComposeCopyCommand() *cobra.Command { 30 usage := `cp [OPTIONS] SERVICE:SRC_PATH DEST_PATH|- 31 nerdctl compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH` 32 var composeCpCommand = &cobra.Command{ 33 Use: usage, 34 Short: "Copy files/folders between a service container and the local filesystem", 35 Args: cobra.ExactArgs(2), 36 RunE: composeCopyAction, 37 SilenceUsage: true, 38 SilenceErrors: true, 39 } 40 composeCpCommand.Flags().Bool("dry-run", false, "Execute command in dry run mode") 41 composeCpCommand.Flags().BoolP("follow-link", "L", false, "Always follow symbol link in SRC_PATH") 42 composeCpCommand.Flags().Int("index", 0, "index of the container if service has multiple replicas") 43 return composeCpCommand 44 } 45 46 func composeCopyAction(cmd *cobra.Command, args []string) error { 47 globalOptions, err := processRootCmdFlags(cmd) 48 if err != nil { 49 return err 50 } 51 source := args[0] 52 if source == "" { 53 return errors.New("source can not be empty") 54 } 55 destination := args[1] 56 if destination == "" { 57 return errors.New("destination can not be empty") 58 } 59 60 dryRun, err := cmd.Flags().GetBool("dry-run") 61 if err != nil { 62 return err 63 } 64 followLink, err := cmd.Flags().GetBool("follow-link") 65 if err != nil { 66 return err 67 } 68 index, err := cmd.Flags().GetInt("index") 69 if err != nil { 70 return err 71 } 72 address := globalOptions.Address 73 // rootless cp runs in the host namespaces, so the address is different 74 if rootlessutil.IsRootless() { 75 address, err = rootlessutil.RootlessContainredSockAddress() 76 if err != nil { 77 return err 78 } 79 } 80 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, address) 81 if err != nil { 82 return err 83 } 84 defer cancel() 85 options, err := getComposeOptions(cmd, globalOptions.DebugFull, globalOptions.Experimental) 86 if err != nil { 87 return err 88 } 89 c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr()) 90 if err != nil { 91 return err 92 } 93 94 co := composer.CopyOptions{ 95 Source: source, 96 Destination: destination, 97 Index: index, 98 FollowLink: followLink, 99 DryRun: dryRun, 100 } 101 return c.Copy(ctx, co) 102 103 }