Simplify the code that creates a tar passed to CopyToContainer (#416)
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user