From 299faa1adff178b78de7771153870ca6bd9a0381 Mon Sep 17 00:00:00 2001 From: knoflook Date: Mon, 15 Nov 2021 16:48:23 +0100 Subject: [PATCH] refactor: move file pulling/pushing logic to internal --- cli/app/cp.go | 60 ++--------------------------------- cli/internal/copy.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 58 deletions(-) create mode 100644 cli/internal/copy.go diff --git a/cli/app/cp.go b/cli/app/cp.go index 16a6f5d2..3dd90174 100644 --- a/cli/app/cp.go +++ b/cli/app/cp.go @@ -5,13 +5,8 @@ import ( "os" "strings" - "coopcloud.tech/abra/cli/formatter" "coopcloud.tech/abra/cli/internal" - "coopcloud.tech/abra/pkg/client" "coopcloud.tech/abra/pkg/config" - "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/filters" - "github.com/docker/docker/pkg/archive" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) @@ -81,63 +76,12 @@ And if you want to copy that file back to your current working directory locally logrus.Fatalf("%s does not exist locally?", dstPath) } } - - appFiles, err := config.LoadAppFiles("") + err := internal.ConfigureAndCp(c, app, srcPath, dstPath, service, isToContainer) if err != nil { logrus.Fatal(err) } - - appEnv, err := config.GetApp(appFiles, app.Name) - if err != nil { - logrus.Fatal(err) - } - - cl, err := client.New(app.Server) - if err != nil { - logrus.Fatal(err) - } - - filters := filters.NewArgs() - filters.Add("name", fmt.Sprintf("%s_%s", appEnv.StackName(), service)) - containers, err := cl.ContainerList(c.Context, types.ContainerListOptions{Filters: filters}) - if err != nil { - logrus.Fatal(err) - } - - if len(containers) != 1 { - logrus.Fatalf("expected 1 container but got %v", len(containers)) - } - container := containers[0] - - logrus.Debugf("retrieved '%s' as target container on '%s'", formatter.ShortenID(container.ID), app.Server) - - if isToContainer { - if _, err := os.Stat(srcPath); err != nil { - logrus.Fatalf("'%s' does not exist?", srcPath) - } - - toTarOpts := &archive.TarOptions{NoOverwriteDirNonDir: true, Compression: archive.Gzip} - content, err := archive.TarWithOptions(srcPath, toTarOpts) - if err != nil { - logrus.Fatal(err) - } - - copyOpts := types.CopyToContainerOptions{AllowOverwriteDirWithFile: false, CopyUIDGID: false} - if err := cl.CopyToContainer(c.Context, container.ID, dstPath, content, copyOpts); err != nil { - logrus.Fatal(err) - } - } else { - content, _, err := cl.CopyFromContainer(c.Context, container.ID, srcPath) - if err != nil { - logrus.Fatal(err) - } - defer content.Close() - fromTarOpts := &archive.TarOptions{NoOverwriteDirNonDir: true, Compression: archive.Gzip} - if err := archive.Untar(content, dstPath, fromTarOpts); err != nil { - logrus.Fatal(err) - } - } return nil + }, BashComplete: func(c *cli.Context) { appNames, err := config.GetAppNames() diff --git a/cli/internal/copy.go b/cli/internal/copy.go new file mode 100644 index 00000000..c89656ac --- /dev/null +++ b/cli/internal/copy.go @@ -0,0 +1,74 @@ +package internal + +import ( + "fmt" + "os" + + "coopcloud.tech/abra/cli/formatter" + "coopcloud.tech/abra/pkg/client" + "coopcloud.tech/abra/pkg/config" + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/pkg/archive" + "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" +) + +func ConfigureAndCp(c *cli.Context, app config.App, srcPath string, dstPath string, service string, isToContainer bool) error { + appFiles, err := config.LoadAppFiles("") + if err != nil { + logrus.Fatal(err) + } + + appEnv, err := config.GetApp(appFiles, app.Name) + if err != nil { + logrus.Fatal(err) + } + + cl, err := client.New(app.Server) + if err != nil { + logrus.Fatal(err) + } + + filters := filters.NewArgs() + filters.Add("name", fmt.Sprintf("%s_%s", appEnv.StackName(), service)) + containers, err := cl.ContainerList(c.Context, types.ContainerListOptions{Filters: filters}) + if err != nil { + logrus.Fatal(err) + } + + if len(containers) != 1 { + logrus.Fatalf("expected 1 container but got %v", len(containers)) + } + container := containers[0] + + logrus.Debugf("retrieved '%s' as target container on '%s'", formatter.ShortenID(container.ID), app.Server) + + if isToContainer { + if _, err := os.Stat(srcPath); err != nil { + logrus.Fatalf("'%s' does not exist?", srcPath) + } + + toTarOpts := &archive.TarOptions{NoOverwriteDirNonDir: true, Compression: archive.Gzip} + content, err := archive.TarWithOptions(srcPath, toTarOpts) + if err != nil { + logrus.Fatal(err) + } + + copyOpts := types.CopyToContainerOptions{AllowOverwriteDirWithFile: false, CopyUIDGID: false} + if err := cl.CopyToContainer(c.Context, container.ID, dstPath, content, copyOpts); err != nil { + logrus.Fatal(err) + } + } else { + content, _, err := cl.CopyFromContainer(c.Context, container.ID, srcPath) + if err != nil { + logrus.Fatal(err) + } + defer content.Close() + fromTarOpts := &archive.TarOptions{NoOverwriteDirNonDir: true, Compression: archive.Gzip} + if err := archive.Untar(content, dstPath, fromTarOpts); err != nil { + logrus.Fatal(err) + } + } + return nil +}