2023-02-12 20:59:55 +00:00
|
|
|
//go:build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-02-24 18:07:08 +00:00
|
|
|
"log"
|
2023-02-12 20:59:55 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2023-02-24 18:07:08 +00:00
|
|
|
|
|
|
|
"github.com/magefile/mage/mg"
|
2023-02-12 20:59:55 +00:00
|
|
|
)
|
|
|
|
|
2023-02-24 18:07:08 +00:00
|
|
|
// 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 == "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()
|
|
|
|
|
2023-02-26 17:14:39 +00:00
|
|
|
if dirName != "compose" {
|
|
|
|
mg.Deps(
|
|
|
|
mg.F(Download, dirName),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-24 18:07:08 +00:00
|
|
|
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(
|
2023-02-26 17:14:39 +00:00
|
|
|
mg.F(Download, name),
|
2023-02-24 18:07:08 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-12 20:59:55 +00:00
|
|
|
func render(cfg config, component string) error {
|
|
|
|
buildDirName := filepath.Join(rootBuildDir, component)
|
|
|
|
if err := os.MkdirAll(buildDirName, 0o750); err != nil {
|
|
|
|
return fmt.Errorf("unable to make %s; %w", buildDirName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
templateDirName := filepath.Join(rootTemplatesDir, component)
|
|
|
|
|
|
|
|
files, err := os.ReadDir(templateDirName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read files from %s; %w ", templateDirName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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).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
|
|
|
|
}
|