From 154bcec95cc207339f05dd4d62e987efb25d0366 Mon Sep 17 00:00:00 2001 From: Sirisha Gopigiri <sirishagopigiri@gmail.com> Date: Thu, 31 Dec 2020 16:12:49 +0530 Subject: [PATCH] Adding Filesystem check function to templater The following code uses the documentFs interface and adds simple fileExists function to templater plugin. Change-Id: Ia53c573e54188960eaf99f7c48469359d4e3688b --- pkg/document/plugin/templater/templater.go | 3 +++ pkg/document/plugin/templater/templater_test.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/pkg/document/plugin/templater/templater.go b/pkg/document/plugin/templater/templater.go index fca4784bb..33ff131de 100644 --- a/pkg/document/plugin/templater/templater.go +++ b/pkg/document/plugin/templater/templater.go @@ -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 diff --git a/pkg/document/plugin/templater/templater_test.go b/pkg/document/plugin/templater/templater_test.go index 9d168d654..07f7e2c74 100644 --- a/pkg/document/plugin/templater/templater_test.go +++ b/pkg/document/plugin/templater/templater_test.go @@ -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 {