more cleanup

This commit is contained in:
p4u1 2023-11-16 22:01:59 +01:00
parent 09ec4b9a81
commit d684b8b92b
1 changed files with 16 additions and 32 deletions

View File

@ -17,7 +17,6 @@ import (
"coopcloud.tech/abra/pkg/upstream/container"
"github.com/docker/cli/cli/command"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
dockerClient "github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/archive"
@ -54,38 +53,28 @@ And if you want to copy that file back to your current working directory locally
dst := c.Args().Get(2)
if src == "" {
logrus.Fatal("missing <src> argument")
} else if dst == "" {
}
if dst == "" {
logrus.Fatal("missing <dest> argument")
}
srcPath, dstPath, service, isToContainer, err := parseSrcAndDst(src, dst)
srcPath, dstPath, service, toContainer, err := parseSrcAndDst(src, dst)
if err != nil {
log.Fatal(err)
}
if isToContainer {
logrus.Debugf("assuming transfer is going TO the container")
} else {
logrus.Debugf("assuming transfer is coming FROM the container")
}
if isToContainer {
if _, err := os.Stat(srcPath); os.IsNotExist(err) {
logrus.Fatalf("%s does not exist locally?", srcPath)
}
}
cl, err := client.New(app.Server)
if err != nil {
logrus.Fatal(err)
}
container, err := findContainer(cl, app.StackName(), service)
container, err := containerPkg.GetContainerFromStackAndService(cl, app.StackName(), service)
if err != nil {
logrus.Fatal(err)
}
logrus.Debugf("retrieved %s as target container on %s", formatter.ShortenID(container.ID), app.Server)
if isToContainer {
if toContainer {
err = copyToContainer(cl, container.ID, srcPath, dstPath)
} else {
err = copyFromContainer(cl, container.ID, srcPath, dstPath)
@ -116,31 +105,25 @@ func parseSrcAndDst(src, dst string) (srcPath string, dstPath string, service st
return "", "", "", false, errServiceMissing
}
func findContainer(cl *dockerClient.Client, stack, service string) (types.Container, error) {
filters := filters.NewArgs()
filters.Add("name", fmt.Sprintf("^%s_%s", stack, service))
container, err := containerPkg.GetContainer(context.Background(), cl, filters, internal.NoInput)
if err != nil {
return types.Container{}, err
}
return container, nil
}
// copyToContainer works with one of the following:
//
// <src_dir>/<file> -> <dst_dir> = <dst_dir>/<file>
// <src_dir>/<file> -> <dst_dir>/<file> = <dst_dir>/<file> (overrides the file)
// <src_dir> -> <dst_dir> = <dst_dir>/<contents_of_src_dir> (creates the dst_dir if it does not exist)
// <file> -> <dst_dir> = <dst_dir>/<file>
// <file> -> <dst_dir>/<file> = <dst_dir>/<file> (overrides the file)
// <src_dir> -> <dst_dir> = <dst_dir>/<src_dir>/<contents_of_src_dir>
// <src_dir>/ -> <dst_dir> = <dst_dir>/<contents_of_src_dir>
func copyToContainer(cl *dockerClient.Client, containerID, srcPath, dstPath string) error {
srcStat, _ := os.Stat(srcPath)
srcStat, err := os.Stat(srcPath)
if err != nil {
return fmt.Errorf("local path: %s", err)
}
dstStat, err := cl.ContainerStatPath(context.Background(), containerID, dstPath)
dstExists := true
if err != nil {
if errdefs.IsNotFound(err) {
dstExists = false
} else {
return fmt.Errorf("stat on remote: %s", err)
return fmt.Errorf("remote path: %s", err)
}
}
@ -187,6 +170,7 @@ func copyToContainer(cl *dockerClient.Client, containerID, srcPath, dstPath stri
return err
}
logrus.Debugf("copy %s from local to %s on container", srcPath, dstPath)
copyOpts := types.CopyToContainerOptions{AllowOverwriteDirWithFile: false, CopyUIDGID: false}
if err := cl.CopyToContainer(context.Background(), containerID, dstPath, content, copyOpts); err != nil {
return err