github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_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 "path/filepath" 22 "strings" 23 24 "github.com/spf13/cobra" 25 ) 26 27 var errFileSpecDoesntMatchFormat = errors.New("filespec must match the canonical format: [container:]file/path") 28 29 func parseCpFileSpec(arg string) (*cpFileSpec, error) { 30 i := strings.Index(arg, ":") 31 32 // filespec starting with a semicolon is invalid 33 if i == 0 { 34 return nil, errFileSpecDoesntMatchFormat 35 } 36 37 if filepath.IsAbs(arg) { 38 // Explicit local absolute path, e.g., `C:\foo` or `/foo`. 39 return &cpFileSpec{ 40 Container: nil, 41 Path: arg, 42 }, nil 43 } 44 45 parts := strings.SplitN(arg, ":", 2) 46 47 if len(parts) == 1 || strings.HasPrefix(parts[0], ".") { 48 // Either there's no `:` in the arg 49 // OR it's an explicit local relative path like `./file:name.txt`. 50 return &cpFileSpec{ 51 Path: arg, 52 }, nil 53 } 54 55 return &cpFileSpec{ 56 Container: &parts[0], 57 Path: parts[1], 58 }, nil 59 } 60 61 type cpFileSpec struct { 62 Container *string 63 Path string 64 } 65 66 func cpShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 67 return nil, cobra.ShellCompDirectiveFilterFileExt 68 }