Skip to content

Commit

Permalink
regex support flag in replacement transformer
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Trojanowski <marcintrojanowski88@gmail.com>
  • Loading branch information
m-trojanowski committed Aug 21, 2023
1 parent 911ddcd commit 5c4876c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/filters/replacement/replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func copyValueToTarget(target *yaml.RNode, value *yaml.RNode, selector *types.Ta
if err != nil {
return errors.WrapPrefixf(err, fieldRetrievalError(fp, createKind != 0))
}
if len(targetFields) == 0 {
if len(targetFields) == 0 && !selector.Options.RegexFindMode {
return errors.Errorf(fieldRetrievalError(fp, createKind != 0))
}

Expand Down
84 changes: 84 additions & 0 deletions api/filters/replacement/replacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,90 @@ func TestFilter(t *testing.T) {
expected string
expectedErr string
}{
"regexsimpleempty": {
input: `apiVersion: v1
kind: Deployment
metadata:
name: deploy
spec:
template:
spec:
containers:
- image: nginx:1.7.9
name: nginx-tagged
- image: postgres:1.8.0
name: postgresdb
`,
replacements: `replacements:
- source:
kind: Deployment
name: deploy
fieldPath: spec.template.spec.containers.0.image
targets:
- select:
kind: Deployment
name: deploy
options:
regexfindmode: true
fieldPaths:
- spec.template.spec.containers.[name=myservice-*].image
`,
expected: `apiVersion: v1
kind: Deployment
metadata:
name: deploy
spec:
template:
spec:
containers:
- image: nginx:1.7.9
name: nginx-tagged
- image: postgres:1.8.0
name: postgresdb
`,
},
"regexsimple": {
input: `apiVersion: v1
kind: Deployment
metadata:
name: deploy
spec:
template:
spec:
containers:
- image: nginx:1.7.9
name: nginx-tagged
- image: postgres:1.8.0
name: myservice-postgresdb
`,
replacements: `replacements:
- source:
kind: Deployment
name: deploy
fieldPath: spec.template.spec.containers.0.image
targets:
- select:
kind: Deployment
name: deploy
options:
regexfindmode: true
fieldPaths:
- spec.template.spec.containers.[name=myservice-*].image
`,
expected: `apiVersion: v1
kind: Deployment
metadata:
name: deploy
spec:
template:
spec:
containers:
- image: nginx:1.7.9
name: nginx-tagged
- image: nginx:1.7.9
name: myservice-postgresdb
`,
},
"simple": {
input: `apiVersion: v1
kind: Deployment
Expand Down
3 changes: 3 additions & 0 deletions api/types/replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type FieldOptions struct {

// If field missing, add it.
Create bool `json:"create,omitempty" yaml:"create,omitempty"`

// Allows to Find Field values using regex primitives.
RegexFindMode bool `json:"regexfindmode,omitempty" yaml:"regexfindmode,omitempty"`
}

func (fo *FieldOptions) String() string {
Expand Down

0 comments on commit 5c4876c

Please sign in to comment.