-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_service.go
116 lines (100 loc) · 3.42 KB
/
render_service.go
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
110
111
112
113
114
115
116
package domain
import (
"errors"
pipeline "github.com/ccremer/go-command-pipeline"
"golang.org/x/sys/unix"
)
// RenderService is a domain service that helps rendering templates.
type RenderService struct {
instrumentation RenderServiceInstrumentation
}
// RenderContext represents a single rendering context for a GitRepository.
type RenderContext struct {
Repository *GitRepository
ValueStore ValueStore
TemplateStore TemplateStore
Engine TemplateEngine
instrumentation RenderServiceInstrumentation
templates []*Template
values Values
}
func NewRenderService(instrumentation RenderServiceInstrumentation) *RenderService {
return &RenderService{
instrumentation: instrumentation,
}
}
// RenderTemplates loads the Templates and renders them in the GitRepository.RootDir of the given RenderContext.Repository.
func (s *RenderService) RenderTemplates(ctx RenderContext) error {
ctx.instrumentation = s.instrumentation.WithRepository(ctx.Repository)
result := pipeline.NewPipeline().WithSteps(
pipeline.NewStep("preflight check", ctx.preFlightCheck()),
pipeline.NewStep("load templates", ctx.toAction(ctx.loadTemplates)),
pipeline.NewStep("render templates", ctx.toAction(ctx.renderTemplates)),
).Run()
return result.Err
}
func (ctx *RenderContext) preFlightCheck() pipeline.ActionFunc {
return func(_ pipeline.Context) pipeline.Result {
err := firstOf(
checkIfArgumentNil(ctx.Engine, "Engine"),
checkIfArgumentNil(ctx.Repository, "Repository"),
checkIfArgumentNil(ctx.TemplateStore, "TemplateStore"),
checkIfArgumentNil(ctx.ValueStore, "ValueStore"),
)
return pipeline.Result{Err: err}
}
}
func (ctx *RenderContext) toAction(action func() error) pipeline.ActionFunc {
return func(_ pipeline.Context) pipeline.Result {
return pipeline.Result{Err: action()}
}
}
func (ctx *RenderContext) renderTemplates() error {
for _, template := range ctx.templates {
if unmanaged, err := ctx.ValueStore.FetchUnmanagedFlag(template, ctx.Repository); err != nil && !errors.Is(err, ErrKeyNotFound) {
return err
} else if unmanaged {
continue
}
if err := ctx.loadValues(template); err != nil {
return err
}
if err := ctx.renderTemplate(template); err != nil {
return err
}
}
return nil
}
func (ctx *RenderContext) renderTemplate(template *Template) error {
// This allows us to create files with 777 permissions
originalUmask := unix.Umask(0)
defer unix.Umask(originalUmask)
ctx.instrumentation.AttemptingToRenderTemplate(template)
alternativePath, err := ctx.ValueStore.FetchTargetPath(template, ctx.Repository)
if err != nil {
return err
}
result, err := template.Render(ctx.values, ctx.Engine)
if err != nil {
return err
}
targetPath := template.RelativePath
if alternativePath != "" {
targetPath = alternativePath
}
err = result.WriteToFile(ctx.Repository.RootDir.Join(targetPath), template.FilePermissions)
return ctx.instrumentation.WrittenRenderResultToFile(template, targetPath, err)
}
func (ctx *RenderContext) loadTemplates() error {
templates, err := ctx.TemplateStore.FetchTemplates()
ctx.templates = templates
return ctx.instrumentation.FetchedTemplatesFromStore(err)
}
func (ctx *RenderContext) loadValues(template *Template) error {
values, err := ctx.ValueStore.FetchValuesForTemplate(template, ctx.Repository)
ctx.values = Values{
"Values": values,
"Metadata": ctx.Repository,
}
return ctx.instrumentation.FetchedValuesForTemplate(err, template)
}