Skip to content

Commit

Permalink
switch to liquid templating to remain consistent w/ api
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino committed Jan 23, 2024
1 parent 29a2735 commit ea47110
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ require (
golang.org/x/mod v0.10.0
golang.org/x/oauth2 v0.8.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.11.2
k8s.io/api v0.26.4
k8s.io/apimachinery v0.26.4
Expand Down Expand Up @@ -284,7 +285,6 @@ require (
gopkg.in/cheggaaa/pb.v1 v1.0.28 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/cluster-bootstrap v0.25.3 // indirect
k8s.io/kops v1.25.2 // indirect
k8s.io/kube-aggregator v0.25.2 // indirect
Expand Down
12 changes: 6 additions & 6 deletions pkg/pr/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func applyUpdates(updates *UpdateSpec, ctx map[string]interface{}) error {
replacement, err := templateReplacement(updates.ReplaceTemplate, ctx)
replacement, err := templateReplacement([]byte(updates.ReplaceTemplate), ctx)
if err != nil {
return err
}
Expand All @@ -34,7 +34,7 @@ func applyUpdates(updates *UpdateSpec, ctx map[string]interface{}) error {
})
}

func updateFile(path string, updates *UpdateSpec, replacement string) error {
func updateFile(path string, updates *UpdateSpec, replacement []byte) error {
switch updates.MatchStrategy {
case "any":
return anyUpdateFile(path, updates, replacement)
Expand All @@ -47,14 +47,14 @@ func updateFile(path string, updates *UpdateSpec, replacement string) error {
}
}

func anyUpdateFile(path string, updates *UpdateSpec, replacement string) error {
func anyUpdateFile(path string, updates *UpdateSpec, replacement []byte) error {
return replaceInPlace(path, func(data []byte) ([]byte, error) {
for _, reg := range updates.Regexes {
r, err := regexp.Compile(reg)
if err != nil {
return data, err
}
data = r.ReplaceAll(data, []byte(replacement))
data = r.ReplaceAll(data, replacement)
}
return data, nil
})
Expand All @@ -64,13 +64,13 @@ func allUpdateFile(path string, updates *UpdateSpec) error {
return nil
}

func recursiveUpdateFile(path string, updates *UpdateSpec, replacement string) error {
func recursiveUpdateFile(path string, updates *UpdateSpec, replacement []byte) error {
return replaceInPlace(path, func(data []byte) ([]byte, error) {
return recursiveReplace(data, updates.Regexes, replacement)
})
}

func recursiveReplace(data []byte, regexes []string, replacement string) ([]byte, error) {
func recursiveReplace(data []byte, regexes []string, replacement []byte) ([]byte, error) {
if len(regexes) == 0 {
return []byte(replacement), nil
}
Expand Down
21 changes: 9 additions & 12 deletions pkg/pr/utils.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package pr

import (
"bytes"
"os"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/osteele/liquid"
)

func templateReplacement(data string, ctx map[string]interface{}) (string, error) {
tpl, err := template.New("gotpl").Funcs(sprig.TxtFuncMap()).Parse(data)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tpl.Execute(&buf, map[string]interface{}{"context": ctx}); err != nil {
return "", err
var (
liquidEngine = liquid.NewEngine()
)

func templateReplacement(data []byte, ctx map[string]interface{}) ([]byte, error) {
bindings := map[string]interface{}{
"context": ctx,
}
return buf.String(), nil
return liquidEngine.ParseAndRender(data, bindings)
}

func replaceInPlace(path string, rep func(data []byte) ([]byte, error)) error {
Expand Down

0 comments on commit ea47110

Please sign in to comment.