/
pipeline.go
56 lines (50 loc) · 1.7 KB
/
pipeline.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package template
import (
"bytes"
"fmt"
"text/template"
)
const (
pipelineCFTemplatePath = "cicd/pipeline_cfn.yml"
fmtPipelinePartialsPath = "cicd/partials/%s.yml"
)
var pipelinePartialTemplateNames = []string{"build-action", "role-policy-document", "role-config", "actions", "action-config", "test"}
// ParsePipeline parses a pipeline's CloudFormation template with the specified data object and returns its content.
func (t *Template) ParsePipeline(data interface{}) (*Content, error) {
tpl, err := t.parse("base", pipelineCFTemplatePath, withPipelineParsingFuncs())
if err != nil {
return nil, err
}
for _, templateName := range pipelinePartialTemplateNames {
nestedTpl, err := t.parse(templateName, fmt.Sprintf(fmtPipelinePartialsPath, templateName), withPipelineParsingFuncs())
if err != nil {
return nil, err
}
_, err = tpl.AddParseTree(templateName, nestedTpl.Tree)
if err != nil {
return nil, fmt.Errorf("add parse tree of %s to base template: %w", templateName, err)
}
}
buf := &bytes.Buffer{}
if err := tpl.Execute(buf, data); err != nil {
return nil, fmt.Errorf("execute pipeline template with data %v: %w", data, err)
}
return &Content{buf}, nil
}
func withPipelineParsingFuncs() ParseOption {
return func(t *template.Template) *template.Template {
return t.Funcs(map[string]interface{}{
"isCodeStarConnection": func(source interface{}) bool {
type connectionName interface {
ConnectionName() (string, error)
}
_, ok := source.(connectionName)
return ok
},
"logicalIDSafe": ReplaceDashesFunc,
"alphanumeric": StripNonAlphaNumFunc,
})
}
}