github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/copy/parse.go (about) 1 package copy 2 3 import ( 4 "strings" 5 6 "github.com/pkg/errors" 7 ) 8 9 // ParseSourceAndDestination parses the source and destination input into a 10 // possibly specified container and path. The input format is described in 11 // podman-cp(1) as "[nameOrID:]path". Colons in paths are supported as long 12 // they start with a dot or slash. 13 // 14 // It returns, in order, the source container and path, followed by the 15 // destination container and path, and an error. Note that exactly one 16 // container must be specified. 17 func ParseSourceAndDestination(source, destination string) (string, string, string, string, error) { 18 sourceContainer, sourcePath := parseUserInput(source) 19 destContainer, destPath := parseUserInput(destination) 20 21 if len(sourcePath) == 0 || len(destPath) == 0 { 22 return "", "", "", "", errors.Errorf("invalid arguments %q, %q: you must specify paths", source, destination) 23 } 24 25 return sourceContainer, sourcePath, destContainer, destPath, nil 26 } 27 28 // parseUserInput parses the input string and returns, if specified, the name 29 // or ID of the container and the path. The input format is described in 30 // podman-cp(1) as "[nameOrID:]path". Colons in paths are supported as long 31 // they start with a dot or slash. 32 func parseUserInput(input string) (container string, path string) { 33 if len(input) == 0 { 34 return 35 } 36 path = input 37 38 // If the input starts with a dot or slash, it cannot refer to a 39 // container. 40 if input[0] == '.' || input[0] == '/' { 41 return 42 } 43 44 if spl := strings.SplitN(path, ":", 2); len(spl) == 2 { 45 container = spl[0] 46 path = spl[1] 47 } 48 return 49 }