//go:build mage package main import ( "fmt" "io" "io/fs" "log" "os" "path/filepath" "strings" "text/template" "github.com/magefile/mage/mg" ) // Prepare prepares the service's build directory. func Prepare(environment, service string) error { cfg, err := newConfig(environment) if err != nil { return fmt.Errorf("unable to load the configuration; %v", err) } if service == "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 } service := o.Name() buildDir := filepath.Join(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 service != "compose" { mg.Deps( mg.F(Download, environment, service), ) log.Printf("Copying assets for %s.\n", service) if err := copyAssets(environment, service); err != nil { return fmt.Errorf("unable to copy the assets for %s; %w", service, err) } } log.Printf("Rendering templates for %s.\n", service) if err := render(cfg, environment, service); err != nil { return fmt.Errorf("unable to render templates for %s; %w", service, err) } } } else { buildDir := filepath.Join(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 service != "compose" { mg.Deps( mg.F(Download, environment, service), mg.F(Prepare, environment, "compose"), ) log.Printf("Copying assets for %s.\n", service) 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 render(cfg config, environment, component string) error { buildDirName := filepath.Join(rootBuildDir, environment, component) templateDirName := filepath.Join(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(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(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 }