Merge "Adding Filesystem check function to templater"

This commit is contained in:
Zuul 2021-01-06 11:10:06 +00:00 committed by Gerrit Code Review
commit 68b1e4ff51
2 changed files with 17 additions and 0 deletions

View File

@ -27,6 +27,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/yaml"
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/fs"
)
var _ kio.Filter = &plugin{}
@ -48,11 +49,13 @@ func New(obj map[string]interface{}) (kio.Filter, error) {
}
func (t *plugin) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) {
docfs := fs.NewDocumentFs()
out := &bytes.Buffer{}
funcMap := sprig.TxtFuncMap()
funcMap["toUint32"] = func(i int) uint32 { return uint32(i) }
funcMap["toYaml"] = toYaml
funcMap["regexGen"] = regexGen
funcMap["fileExists"] = docfs.Exists
tmpl, err := template.New("tmpl").Funcs(funcMap).Parse(t.Template)
if err != nil {
return nil, err

View File

@ -181,6 +181,20 @@ template: |
"at <regexGen .regex (.limit | int)>: error calling " +
"regexGen: error parsing regexp: missing closing ]: `[a-z`",
},
{
cfg: `
apiVersion: airshipit.org/v1alpha1
kind: Templater
metadata:
name: notImportantHere
template: |
FileExists: {{ fileExists "./templater.go" }}
NoFileExists: {{ fileExists "./templater1.go" }}
`,
expectedOut: `FileExists: true
NoFileExists: false
`,
},
}
for _, tc := range testCases {