feat: restructure writing to the file

This commit is contained in:
kolaente 2021-12-05 13:24:06 +01:00
parent 861824dbfe
commit 3fc3b8cb18
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 12 additions and 23 deletions

35
save.go
View File

@ -4,21 +4,14 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io"
"os"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/stdcopy"
"io"
"os"
) )
func runAndSaveCommandInContainer(filename string, c *client.Client, container *types.ContainerJSON, command string, args ...string) error { func runAndSaveCommandInContainer(filename string, c *client.Client, container *types.ContainerJSON, command string, args ...string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
ctx := context.Background() ctx := context.Background()
config := types.ExecConfig{ config := types.ExecConfig{
@ -48,27 +41,23 @@ func runAndSaveCommandInContainer(filename string, c *client.Client, container *
outputDone <- err outputDone <- err
}() }()
select { err = <-outputDone
case err := <-outputDone:
if err != nil {
return err
}
break
case <-ctx.Done():
return ctx.Err()
}
_, err = c.ContainerExecInspect(ctx, r.ID)
if err != nil { if err != nil {
fmt.Printf(errBuf.String()) fmt.Printf(errBuf.String())
return err return err
} }
_, err = io.Copy(f, &outBuf) _, err = c.ContainerExecInspect(ctx, r.ID)
if err != nil { if err != nil {
return err return err
} }
return nil f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, &outBuf)
return err
} }