From 9d2d5f36cc60934992da19cebb96b3f643c00b57 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sat, 25 Nov 2023 15:52:25 +0000 Subject: [PATCH] refactor: move deploy code to internal package Move the deploy code to an internal package in preparation for the support for multiple mage targets deploying services to the Flow Platform. Add an option to deploy a service in the foreground. --- internal/deploy/deploy.go | 32 ++++++++++++++++++++++++++++++++ magefiles/magefile.go | 25 +++++++------------------ 2 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 internal/deploy/deploy.go diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go new file mode 100644 index 0000000..269fc23 --- /dev/null +++ b/internal/deploy/deploy.go @@ -0,0 +1,32 @@ +package deploy + +import ( + "flow/services/internal" + "fmt" + "os" + + "github.com/magefile/mage/sh" +) + +func Deploy(dockerHost, environment, name string, daemon bool) error { + os.Setenv("DOCKER_HOST", dockerHost) + + command := []string{ + "docker", + "compose", + "--project-directory", + fmt.Sprintf("%s/%s/compose", internal.RootBuildDir, environment), + "up", + "--build", + } + + if daemon { + command = append(command, "-d") + } + + if name != "all" { + command = append(command, name) + } + + return sh.RunV(command[0], command[1:]...) +} diff --git a/magefiles/magefile.go b/magefiles/magefile.go index 9480af9..00760be 100644 --- a/magefiles/magefile.go +++ b/magefiles/magefile.go @@ -5,6 +5,7 @@ package main import ( "flow/services/internal" "flow/services/internal/config" + "flow/services/internal/deploy" "flow/services/internal/prepare" "fmt" "os" @@ -36,10 +37,10 @@ func Clean(environment string) error { return nil } -// Deploy deploys the services to the Flow Platform. -func Deploy(environment, name string) error { +// Deploy deploys the service(s) to the Flow Platform. +func Deploy(environment, service string) error { mg.Deps( - mg.F(Prepare, environment, name), + mg.F(Prepare, environment, service), ) cfg, err := config.NewConfig(environment) @@ -47,23 +48,11 @@ func Deploy(environment, name string) error { return fmt.Errorf("unable to load the configuration; %w", err) } - os.Setenv("DOCKER_HOST", cfg.Docker.Host) - - command := []string{ - "docker", - "compose", - "--project-directory", - fmt.Sprintf("%s/%s/compose", internal.RootBuildDir, environment), - "up", - "-d", - "--build", + if err := deploy.Deploy(cfg.Docker.Host, environment, service, true); err != nil { + return fmt.Errorf("error deploying %q; %w", service, err) } - if name != "all" { - command = append(command, name) - } - - return sh.RunV(command[0], command[1:]...) + return nil } // Prepare prepares the service's build directory.