-
Notifications
You must be signed in to change notification settings - Fork 3
/
controller.go
73 lines (60 loc) · 1.85 KB
/
controller.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
package halfpipe
import (
"github.com/springernature/halfpipe/defaults"
"github.com/springernature/halfpipe/linters"
"github.com/springernature/halfpipe/manifest"
"github.com/springernature/halfpipe/mapper"
"github.com/springernature/halfpipe/project"
)
type Controller interface {
Process(man manifest.Manifest) (Response, error)
DefaultAndMap(man manifest.Manifest) (updated manifest.Manifest, err error)
}
type Response struct {
ConfigYaml string
Project project.Data
LintResults linters.LintResults
Platform manifest.Platform
}
func (r Response) String() string {
return r.ConfigYaml
}
type Renderer interface {
Render(manifest manifest.Manifest) (string, error)
}
type controller struct {
defaulter defaults.Defaults
mapper mapper.Mapper
linters []linters.Linter
renderer Renderer
}
func NewController(defaulter defaults.Defaults, mapper mapper.Mapper, linters []linters.Linter, renderer Renderer) Controller {
return controller{
defaulter: defaulter,
mapper: mapper,
linters: linters,
renderer: renderer,
}
}
func (c controller) Process(man manifest.Manifest) (response Response, err error) {
defaultedManifest := c.defaulter.Apply(man)
for _, linter := range c.linters {
response.LintResults = append(response.LintResults, linter.Lint(defaultedManifest))
}
if response.LintResults.HasErrors() {
return
}
mappedManifest, err := c.mapper.Apply(defaultedManifest)
if err != nil {
response.LintResults = append(response.LintResults, linters.NewLintResult("Internal mapper", "", []error{err}))
return
}
config, err := c.renderer.Render(mappedManifest)
response.ConfigYaml = config
response.Project = c.defaulter.Project
response.Platform = man.Platform
return
}
func (c controller) DefaultAndMap(man manifest.Manifest) (updated manifest.Manifest, err error) {
return c.mapper.Apply(c.defaulter.Apply(man))
}