From 1a1eb34a29695e1e0d11575cb15d7f7db0c7224b Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Fri, 4 Sep 2020 13:50:19 +0100 Subject: [PATCH] Simplify the code that creates a tar passed to CopyToContainer (#416) --- docker/docker.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index a2025ec..fc8d80c 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -214,19 +214,22 @@ func (d *docker) CreateAttachConnection(name string) (net.Conn, error) { } func (d *docker) CopyToContainer(containerName, destination, fileName string, content io.Reader) error { - r, w := io.Pipe() - b, readErr := ioutil.ReadAll(content) - if readErr != nil { - return readErr + contents, err := ioutil.ReadAll(content) + if err != nil { + return err } - t := tar.NewWriter(w) - go func() { - t.WriteHeader(&tar.Header{Name: fileName, Mode: 0600, Size: int64(len(b)), ModTime: time.Now()}) - t.Write(b) - t.Close() - w.Close() - }() - return d.c.CopyToContainer(context.Background(), containerName, destination, r, types.CopyToContainerOptions{AllowOverwriteDirWithFile: true}) + var buf bytes.Buffer + t := tar.NewWriter(&buf) + if err := t.WriteHeader(&tar.Header{Name: fileName, Mode: 0600, Size: int64(len(contents)), ModTime: time.Now()}); err != nil { + return err + } + if _, err := t.Write(contents); err != nil { + return err + } + if err := t.Close(); err != nil { + return err + } + return d.c.CopyToContainer(context.Background(), containerName, destination, &buf, types.CopyToContainerOptions{AllowOverwriteDirWithFile: true}) } func (d *docker) CopyFromContainer(containerName, filePath string) (io.Reader, error) {