diff --git a/magefiles/deploy.go b/magefiles/deploy.go new file mode 100644 index 0000000..a9532b6 --- /dev/null +++ b/magefiles/deploy.go @@ -0,0 +1,41 @@ +//go:build mage + +package main + +import ( + "fmt" + "os" + + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +// Deploy deploys the services to the Flow Platform. +func Deploy(name string) error { + mg.Deps( + mg.F(Render, name), + ) + + cfg, err := newConfig(configFile) + if err != nil { + return fmt.Errorf("unable to load the configuration; %w", err) + } + + os.Setenv("DOCKER_HOST", cfg.DockerHost) + + command := []string{ + "docker", + "compose", + "--project-directory", + rootBuildDir+"/compose", + "up", + "-d", + "--build", + } + + if name != "all" { + command = append(command, name) + } + + return sh.Run(command[0], command[1:]...) +} diff --git a/magefiles/mage.go b/magefiles/mage.go index 7b33d7f..8ed2c3a 100644 --- a/magefiles/mage.go +++ b/magefiles/mage.go @@ -7,14 +7,13 @@ import ( "os" "github.com/magefile/mage/sh" - "github.com/magefile/mage/mg" ) const ( - configFile string = "./config/services.json" - rootBuildDir string = "./build" - templateExtension string = ".gotmpl" - rootTemplatesDir string = "./templates" + configFile string = "./config/services.json" + rootBuildDir string = "./build" + templateExtension string = ".gotmpl" + rootTemplatesDir string = "./templates" ) // Clean cleans the workspace. @@ -39,51 +38,6 @@ func Clean() error { return nil } -// Render renders the template files. -func Render(name string) error { - if name == "forgejo" { - mg.Deps(DownloadForgejo) - } - - cfg, err := newConfig(configFile) - if err != nil { - return fmt.Errorf("unable to load the configuration; %v", err) - } - - if name != "compose" { - if err := render(cfg, "compose"); err != nil { - return fmt.Errorf("an error occurred whilst rendering the compose file; %w", err) - } - } - - if err := render(cfg, name); err != nil { - return fmt.Errorf("an error occurred whilst rendering the templates; %w", err) - } - - return nil -} - -// Deploy deploys the services to the Flow Platform. -func Deploy(name string) error { - cfg, err := newConfig(configFile) - if err != nil { - return fmt.Errorf("unable to load the configuration; %w", err) - } - - os.Setenv("DOCKER_HOST", cfg.DockerHost) - - return sh.Run( - "docker", - "compose", - "--project-directory", - rootBuildDir+"/compose", - "up", - "-d", - "--build", - name, - ) -} - // DownloadForgejo downloads the Forgejo binary from Codeberg. func DownloadForgejo() error { cfg, err := newConfig(configFile) diff --git a/magefiles/render.go b/magefiles/render.go index 2c1db6c..b716bd7 100644 --- a/magefiles/render.go +++ b/magefiles/render.go @@ -4,12 +4,59 @@ package main import ( "fmt" + "log" "os" "path/filepath" "strings" "text/template" + + "github.com/magefile/mage/mg" ) +// Render renders the template files. +func Render(name string) error { + cfg, err := newConfig(configFile) + if err != nil { + return fmt.Errorf("unable to load the configuration; %v", err) + } + + if name == "forgejo" || name == "all" { + mg.Deps(DownloadForgejo) + } + + if name == "all" { + objects, err := os.ReadDir(rootTemplatesDir) + if err != nil { + return fmt.Errorf("unable to read the templates directory; %w", err) + } + + for _, o := range objects { + if !o.IsDir() { + continue + } + + dirName := o.Name() + + log.Printf("Rendering templates for %s.\n", dirName) + if err := render(cfg, o.Name()); err != nil { + return fmt.Errorf("unable to render templates for %s; %w", dirName, err) + } + } + } else { + if name != "compose" { + mg.Deps( + mg.F(Render, "compose"), + ) + } + + if err := render(cfg, name); err != nil { + return fmt.Errorf("an error occurred whilst rendering the templates; %w", err) + } + } + + return nil +} + func render(cfg config, component string) error { buildDirName := filepath.Join(rootBuildDir, component) if err := os.MkdirAll(buildDirName, 0o750); err != nil {