From 91b2164a4f2d3e75267cfaf6751e2e82db830d4a Mon Sep 17 00:00:00 2001 From: Sirisha Gopigiri Date: Wed, 16 Dec 2020 18:00:54 +0530 Subject: [PATCH] Adding Regex support for Templater Adding go code to templater to support generation of strings based on the regular expression passed. Closes: #438 Change-Id: Ife0c4f8d5dd8032408875c73ba098919470be9e9 --- go.mod | 1 + go.sum | 2 + pkg/document/plugin/templater/templater.go | 14 +++++ .../plugin/templater/templater_test.go | 51 +++++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/go.mod b/go.mod index e9592e412..c19b567a2 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/gorilla/mux v1.7.4 // indirect github.com/gregjones/httpcache v0.0.0-20190212212710-3befbb6ad0cc // indirect github.com/hashicorp/go-cleanhttp v0.5.1 // indirect + github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb github.com/morikuni/aec v1.0.0 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index e8e9a94a8..f4e479d42 100644 --- a/go.sum +++ b/go.sum @@ -474,6 +474,8 @@ github.com/lucas-clemente/aes12 v0.0.0-20171027163421-cd47fb39b79f/go.mod h1:JpH github.com/lucas-clemente/quic-clients v0.1.0/go.mod h1:y5xVIEoObKqULIKivu+gD/LU90pL73bTdtQjPBvtCBk= github.com/lucas-clemente/quic-go v0.10.2/go.mod h1:hvaRS9IHjFLMq76puFJeWNfmn+H70QZ/CXoxqw9bzao= github.com/lucas-clemente/quic-go-certificates v0.0.0-20160823095156-d2f86524cced/go.mod h1:NCcRLrOTZbzhZvixZLlERbJtDtYsmMw8Jc4vS8Z0g58= +github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb h1:w1g9wNDIE/pHSTmAaUhv4TZQuPBS6GV3mMz5hkgziIU= +github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb/go.mod h1:5ELEyG+X8f+meRWHuqUOewBOhvHkl7M76pdGEansxW4= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= diff --git a/pkg/document/plugin/templater/templater.go b/pkg/document/plugin/templater/templater.go index 3d6fef521..fca4784bb 100644 --- a/pkg/document/plugin/templater/templater.go +++ b/pkg/document/plugin/templater/templater.go @@ -20,6 +20,7 @@ import ( "text/template" sprig "github.com/Masterminds/sprig/v3" + "github.com/lucasjones/reggen" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/kustomize/kyaml/kio" @@ -51,6 +52,7 @@ func (t *plugin) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) { funcMap := sprig.TxtFuncMap() funcMap["toUint32"] = func(i int) uint32 { return uint32(i) } funcMap["toYaml"] = toYaml + funcMap["regexGen"] = regexGen tmpl, err := template.New("tmpl").Funcs(funcMap).Parse(t.Template) if err != nil { return nil, err @@ -75,6 +77,18 @@ func (t *plugin) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) { return append(items, res.Nodes...), nil } +// Generate Regex +func regexGen(regex string, limit int) string { + if limit <= 0 { + panic("Limit cannot be less than or equal to 0") + } + str, err := reggen.Generate(regex, limit) + if err != nil { + panic(err) + } + return str +} + // Render input yaml as output yaml // This function is from the Helm project: // https://github.com/helm/helm diff --git a/pkg/document/plugin/templater/templater_test.go b/pkg/document/plugin/templater/templater_test.go index a03a1df09..9d168d654 100644 --- a/pkg/document/plugin/templater/templater_test.go +++ b/pkg/document/plugin/templater/templater_test.go @@ -130,6 +130,57 @@ template: | expectedOut: `touint32: 10 `, }, + { + cfg: ` +apiVersion: airshipit.org/v1alpha1 +kind: Templater +metadata: + name: notImportantHere +values: + regex: "^[a-z]{5,10}$" + nomatchregex: "^[a-z]{0,4}$" + limit: 10 +template: | + truepassword: {{ regexMatch .regex (regexGen .regex (.limit|int)) }} + falsepassword: {{ regexMatch .nomatchregex (regexGen .regex (.limit|int)) }} +`, + expectedOut: `truepassword: true +falsepassword: false +`, + }, { + cfg: ` +apiVersion: airshipit.org/v1alpha1 +kind: Templater +metadata: + name: notImportantHere +values: + name: test + regex: "^[a-z]{5,10}$" + limit: 0 +template: | + password: {{ regexGen .regex (.limit|int) }} +`, + expectedErr: "template: tmpl:1:13: executing \"tmpl\" at " + + ": error calling regexGen: " + + "Limit cannot be less than or equal to 0", + }, + { + cfg: ` +apiVersion: airshipit.org/v1alpha1 +kind: Templater +metadata: + name: notImportantHere +values: + name: test + regex: "^[a-z" + limit: 10 +template: | + password: {{ regexGen .regex (.limit|int) }} +`, + expectedErr: "template: tmpl:1:13: executing \"tmpl\" " + + "at : error calling " + + "regexGen: error parsing regexp: missing closing ]: `[a-z`", + }, } for _, tc := range testCases {