services/internal/prepare/prepare.go
Dan Anglin 25325e2856
refactor: update project structure
Changes:

- Most of the go code is now located in internal packages.
- Code refactored and simplified in some cases.
- Removed the 'download' mage target and integrated the download code
  into the internal 'prepare' package.
- Moved all mage target code to magefile.go.
- Added missing descriptions to the mage targets.
- Updated go.mod.

Fixed:

- Created a custom function to validate the checksum of the
  downloaded Woodpecker tar file.
- Specified the environment when running 'clean'.
2023-11-24 09:56:35 +00:00

249 lines
6.3 KiB
Go

package prepare
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
"flow/services/internal"
"flow/services/internal/config"
"flow/services/internal/services"
"flow/services/internal/services/forgejo"
"flow/services/internal/services/gotosocial"
"flow/services/internal/services/woodpecker"
)
const templateExtension string = ".gotmpl"
func Prepare(environment, service string) error {
cfg, err := config.NewConfig(environment)
if err != nil {
return fmt.Errorf("unable to load the configuration; %w", err)
}
if err := prepareCompose(cfg, environment); err != nil {
return fmt.Errorf("unable to prepare the compose file; %w", err)
}
if service == "all" {
if err := prepareAllServices(cfg, environment); err != nil {
return fmt.Errorf("error preparing the services; %w", err)
}
} else {
if err := prepareService(cfg, environment, service); err != nil {
return fmt.Errorf("error preparing %q; %w", service, err)
}
}
return nil
}
func prepareCompose(cfg config.Config, environment string) error {
compose := "compose"
buildDir := filepath.Join(internal.RootBuildDir, environment, compose)
if _, err := os.Stat(buildDir); err != nil {
if err := os.MkdirAll(buildDir, 0o700); err != nil {
return fmt.Errorf("unable to make %s; %w", buildDir, err)
}
}
if err := render(cfg, environment, compose); err != nil {
return fmt.Errorf("an error occurred whilst rendering the templates; %w", err)
}
return nil
}
func prepareAllServices(cfg config.Config, environment string) error {
allServices := services.All()
for i := range allServices {
if err := prepareService(cfg, environment, allServices[i]); err != nil {
return fmt.Errorf("error preparing %q; %w", allServices[i], err)
}
}
return nil
}
func prepareService(cfg config.Config, environment, service string) error {
buildDir := filepath.Join(internal.RootBuildDir, environment, service)
if _, err := os.Stat(buildDir); err != nil {
if err := os.MkdirAll(buildDir, 0o700); err != nil {
return fmt.Errorf("unable to make %s; %w", buildDir, err)
}
}
if err := downloadServiceFiles(cfg, environment, service); err != nil {
return fmt.Errorf("error downloading service files for %q; %w", service, err)
}
if err := copyAssets(environment, service); err != nil {
return fmt.Errorf("unable to copy the assets for %s; %w", service, err)
}
if err := render(cfg, environment, service); err != nil {
return fmt.Errorf("an error occurred whilst rendering the templates; %w", err)
}
return nil
}
func downloadServiceFiles(cfg config.Config, environment, service string) error {
switch service {
case services.Forgejo:
obj := forgejo.NewForgejo(environment, cfg.Forgejo.Version)
if err := obj.Download(); err != nil {
return fmt.Errorf("error downloading the files for %q; %w", services.Forgejo, err)
}
case services.Gotosocial:
obj := gotosocial.NewGotosocial(environment, cfg.GoToSocial.Version)
if err := obj.Download(); err != nil {
return fmt.Errorf("error downloading the files for %q; %w", services.Gotosocial, err)
}
case services.Woodpecker:
obj := woodpecker.NewWoodpecker(environment, cfg.Woodpecker.Version)
if err := obj.Download(); err != nil {
return fmt.Errorf("error downloading the files for %q; %w", services.Woodpecker, err)
}
default:
fmt.Printf("There's no files to download for %q.\n", service)
}
return nil
}
func render(cfg config.Config, environment, component string) error {
buildDirName := filepath.Join(internal.RootBuildDir, environment, component)
templateDirName := filepath.Join(internal.RootTemplatesDir, component)
_, err := os.Stat(templateDirName)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("There's no template directory for %q.\n", component)
return nil
}
return err
}
files, err := os.ReadDir(templateDirName)
if err != nil {
return fmt.Errorf("unable to read files from %s; %w ", templateDirName, err)
}
funcMap := template.FuncMap{
"default": defaultValue,
}
for _, f := range files {
err := func() error {
templateFilename := f.Name()
if f.IsDir() || !strings.HasSuffix(templateFilename, templateExtension) {
return nil
}
outputFilename := strings.TrimSuffix(templateFilename, templateExtension)
outputPath := filepath.Join(buildDirName, outputFilename)
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("unable to create the file '%s'; %w", outputPath, err)
}
defer file.Close()
templatePath := filepath.Join(templateDirName, templateFilename)
tmpl, err := template.New(templateFilename).Funcs(funcMap).ParseFiles(templatePath)
if err != nil {
return fmt.Errorf("unable to create a new template value from '%s'; %w", templateFilename, err)
}
if err = tmpl.Execute(file, cfg); err != nil {
return fmt.Errorf("unable to render the template to '%s'; %w", outputPath, err)
}
return nil
}()
if err != nil {
return fmt.Errorf("an error occurred whilst rendering the templates for '%s'; %w", component, err)
}
}
return nil
}
func copyAssets(environment, service string) error {
assetsDirName := filepath.Join(internal.RootAssetsDir, service)
if _, err := os.Stat(assetsDirName); err != nil {
if os.IsNotExist(err) {
fmt.Printf("There's no assets directory for %q.\n", service)
return nil
}
return err
}
buildDirName := filepath.Join(internal.RootBuildDir, environment, service, "assets")
walkDirFunc := func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == assetsDirName {
return nil
}
buildAssetPath := filepath.Join(buildDirName, strings.TrimPrefix(path, assetsDirName))
if d.IsDir() {
if err := os.MkdirAll(buildAssetPath, 0o750); err != nil {
return fmt.Errorf("unable to make %s; %w", path, err)
}
return nil
}
source, err := os.Open(path)
if err != nil {
return err
}
defer source.Close()
dest, err := os.Create(buildAssetPath)
if err != nil {
return err
}
defer dest.Close()
if _, err := io.Copy(dest, source); err != nil {
return err
}
return nil
}
err := filepath.WalkDir(assetsDirName, walkDirFunc)
if err != nil {
return fmt.Errorf("error walking the path '%s'; %w", assetsDirName, err)
}
return nil
}
func defaultValue(value, defaultValue string) string {
if value == "" {
return defaultValue
}
return value
}