github.com/tsuna/docker@v1.7.0-rc3/api/client/cp.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "io" 6 "strings" 7 8 "github.com/docker/docker/api/types" 9 "github.com/docker/docker/pkg/archive" 10 flag "github.com/docker/docker/pkg/mflag" 11 ) 12 13 // CmdCp copies files/folders from a path on the container to a directory on the host running the command. 14 // 15 // If HOSTDIR is '-', the data is written as a tar file to STDOUT. 16 // 17 // Usage: docker cp CONTAINER:PATH HOSTDIR 18 func (cli *DockerCli) CmdCp(args ...string) error { 19 cmd := cli.Subcmd("cp", "CONTAINER:PATH HOSTDIR|-", "Copy files/folders from a PATH on the container to a HOSTDIR on the host\nrunning the command. Use '-' to write the data as a tar file to STDOUT.", true) 20 cmd.Require(flag.Exact, 2) 21 22 cmd.ParseFlags(args, true) 23 24 // deal with path name with `:` 25 info := strings.SplitN(cmd.Arg(0), ":", 2) 26 27 if len(info) != 2 { 28 return fmt.Errorf("Error: Path not specified") 29 } 30 31 cfg := &types.CopyConfig{ 32 Resource: info[1], 33 } 34 stream, statusCode, err := cli.call("POST", "/containers/"+info[0]+"/copy", cfg, nil) 35 if stream != nil { 36 defer stream.Close() 37 } 38 if statusCode == 404 { 39 return fmt.Errorf("No such container: %v", info[0]) 40 } 41 if err != nil { 42 return err 43 } 44 45 hostPath := cmd.Arg(1) 46 if statusCode == 200 { 47 if hostPath == "-" { 48 _, err = io.Copy(cli.out, stream) 49 } else { 50 err = archive.Untar(stream, hostPath, &archive.TarOptions{NoLchown: true}) 51 } 52 if err != nil { 53 return err 54 } 55 } 56 return nil 57 }