-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.go
More file actions
109 lines (88 loc) · 2.76 KB
/
templates.go
File metadata and controls
109 lines (88 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package scaffold
import (
"embed"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"slices"
"strings"
"text/template"
"github.com/launchrctl/launchr/pkg/action"
)
//go:embed templates/*
var templateFS embed.FS
const templatesFilesDir = "templates/files"
const templatesDefinitionDir = "templates/definition"
// templateManager orchestrates a template collection, preparation and delivery
type templateManager struct{}
// getTemplateSubdirectories returns all subdirectories within a given path in the embedded filesystem
func (t *templateManager) getTemplateSubdirectories(dirPath string) ([]string, error) {
entries, err := fs.ReadDir(templateFS, filepath.Join(templatesFilesDir, dirPath))
if err != nil {
return nil, fmt.Errorf("failed to read embedded directory %s: %w", dirPath, err)
}
var directories []string
for _, entry := range entries {
if entry.IsDir() {
subDirPath := path.Join(dirPath, entry.Name())
directories = append(directories, subDirPath)
subDirs, err := t.getTemplateSubdirectories(subDirPath)
if err != nil {
return nil, err
}
directories = append(directories, subDirs...)
}
}
directories = append(directories, dirPath)
return directories, nil
}
func (t *templateManager) renderTemplates(output string, values *templateValues, templates []*template.Template) error {
for _, t := range templates {
outputPath := filepath.Clean(filepath.Join(output, strings.Replace(t.Name(), ".tmpl", "", 1)))
outFile, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("failed to create output file %s: %w", outputPath, err)
}
err = t.Execute(outFile, values)
if err != nil {
return err
}
_ = outFile.Close()
}
return nil
}
// getDefinitionTemplate creates the action.yaml file from templates
func (t *templateManager) getDefinitionTemplate(runtimeType action.DefRuntimeType) (*template.Template, error) {
tmpl, err := template.New("action.yaml").
ParseFS(templateFS,
filepath.Join(templatesDefinitionDir, "action.yaml.tmpl"),
filepath.Join(templatesDefinitionDir, fmt.Sprintf("%s.yaml.tmpl", runtimeType)),
)
if err != nil {
return nil, err
}
var combined string
var names []string
for _, t := range tmpl.Templates() {
names = append(names, fmt.Sprintf("{{template \"%s\" .}}", t.Name()))
}
slices.Sort(names)
combined = strings.Join(names, "\n")
combinedTmpl, err := tmpl.New("action.yaml").Parse(combined)
if err != nil {
return nil, err
}
return combinedTmpl, err
}
func (t *templateManager) getRuntimeTemplates(dir string) ([]*template.Template, error) {
tmpl := template.New("")
var err error
patterns := []string{filepath.Join(templatesFilesDir, dir, "*.tmpl")}
tmpl, err = tmpl.ParseFS(templateFS, patterns...)
if err != nil {
return nil, err
}
return tmpl.Templates(), err
}