fix: directly write dump to file without buffering in memory
continuous-integration/drone/push Build is passing Details

Resolves #5
This commit is contained in:
kolaente 2024-01-28 23:05:08 +01:00
parent 1b17dedc47
commit 5982f1ef07
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 12 additions and 26 deletions

38
save.go
View File

@ -1,14 +1,13 @@
package main
import (
"bytes"
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"io"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func runAndSaveCommandInContainer(filename string, c *client.Client, container *types.ContainerJSON, command string, args ...string) error {
@ -31,33 +30,20 @@ func runAndSaveCommandInContainer(filename string, c *client.Client, container *
}
defer resp.Close()
// read the output
var outBuf, errBuf bytes.Buffer
outputDone := make(chan error)
go func() {
// StdCopy demultiplexes the stream into two buffers
_, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
outputDone <- err
}()
err = <-outputDone
if err != nil {
fmt.Printf(errBuf.String())
return err
}
_, err = c.ContainerExecInspect(ctx, r.ID)
if err != nil {
return err
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, &outBuf)
_, err = io.Copy(f, resp.Reader)
if err != nil {
return err
}
execInspect, err := c.ContainerExecInspect(ctx, r.ID)
if execInspect.ExitCode != 0 {
return fmt.Errorf("backup from container %s failed with exit code %d", container.Name, execInspect.ExitCode)
}
return err
}