Adding Filesystem check function to templater

The following code uses the documentFs interface and adds
simple fileExists function to templater plugin.

Change-Id: Ia53c573e54188960eaf99f7c48469359d4e3688b
This commit is contained in:
Sirisha Gopigiri 2020-12-31 16:12:49 +05:30
parent 1016a29870
commit 154bcec95c
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 {