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
This commit is contained in:
parent
1890d0cd7e
commit
91b2164a4f
1
go.mod
1
go.mod
@ -23,6 +23,7 @@ require (
|
|||||||
github.com/gorilla/mux v1.7.4 // indirect
|
github.com/gorilla/mux v1.7.4 // indirect
|
||||||
github.com/gregjones/httpcache v0.0.0-20190212212710-3befbb6ad0cc // indirect
|
github.com/gregjones/httpcache v0.0.0-20190212212710-3befbb6ad0cc // indirect
|
||||||
github.com/hashicorp/go-cleanhttp v0.5.1 // 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/morikuni/aec v1.0.0 // indirect
|
||||||
github.com/opencontainers/image-spec v1.0.1 // indirect
|
github.com/opencontainers/image-spec v1.0.1 // indirect
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
|
2
go.sum
2
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-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 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/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.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||||
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
|
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
|
||||||
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
sprig "github.com/Masterminds/sprig/v3"
|
sprig "github.com/Masterminds/sprig/v3"
|
||||||
|
"github.com/lucasjones/reggen"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
@ -51,6 +52,7 @@ func (t *plugin) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) {
|
|||||||
funcMap := sprig.TxtFuncMap()
|
funcMap := sprig.TxtFuncMap()
|
||||||
funcMap["toUint32"] = func(i int) uint32 { return uint32(i) }
|
funcMap["toUint32"] = func(i int) uint32 { return uint32(i) }
|
||||||
funcMap["toYaml"] = toYaml
|
funcMap["toYaml"] = toYaml
|
||||||
|
funcMap["regexGen"] = regexGen
|
||||||
tmpl, err := template.New("tmpl").Funcs(funcMap).Parse(t.Template)
|
tmpl, err := template.New("tmpl").Funcs(funcMap).Parse(t.Template)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -75,6 +77,18 @@ func (t *plugin) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) {
|
|||||||
return append(items, res.Nodes...), nil
|
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
|
// Render input yaml as output yaml
|
||||||
// This function is from the Helm project:
|
// This function is from the Helm project:
|
||||||
// https://github.com/helm/helm
|
// https://github.com/helm/helm
|
||||||
|
@ -130,6 +130,57 @@ template: |
|
|||||||
expectedOut: `touint32: 10
|
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 " +
|
||||||
|
"<regexGen .regex (.limit | int)>: 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 <regexGen .regex (.limit | int)>: error calling " +
|
||||||
|
"regexGen: error parsing regexp: missing closing ]: `[a-z`",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
Loading…
Reference in New Issue
Block a user