Simplify the code that creates a tar passed to CopyToContainer (#416)

This commit is contained in:
Paul Jolly
2020-09-04 13:50:19 +01:00
committed by GitHub
parent 681de41e0a
commit 1a1eb34a29

View File

@@ -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 { func (d *docker) CopyToContainer(containerName, destination, fileName string, content io.Reader) error {
r, w := io.Pipe() contents, err := ioutil.ReadAll(content)
b, readErr := ioutil.ReadAll(content) if err != nil {
if readErr != nil { return err
return readErr
} }
t := tar.NewWriter(w) var buf bytes.Buffer
go func() { t := tar.NewWriter(&buf)
t.WriteHeader(&tar.Header{Name: fileName, Mode: 0600, Size: int64(len(b)), ModTime: time.Now()}) if err := t.WriteHeader(&tar.Header{Name: fileName, Mode: 0600, Size: int64(len(contents)), ModTime: time.Now()}); err != nil {
t.Write(b) return err
t.Close() }
w.Close() if _, err := t.Write(contents); err != nil {
}() return err
return d.c.CopyToContainer(context.Background(), containerName, destination, r, types.CopyToContainerOptions{AllowOverwriteDirWithFile: true}) }
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) { func (d *docker) CopyFromContainer(containerName, filePath string) (io.Reader, error) {