Skip to content

Commit

Permalink
Invert arg order for many liquid filters (#175)
Browse files Browse the repository at this point in the history
Using the sprig library can create an unfortunate devex since pipe arguments work differently in text/template vs liquid.  This should solve for that.
  • Loading branch information
michaeljguarino authored Apr 29, 2024
1 parent ea6d0c8 commit eb29e2c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/manifests/template/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ var (
"sha256sum": "sha26sum",
"quote": "quote",
"squote": "squote",
"indent": "indent",
"nindent": "nindent",
"replace": "replace",
"default": "default",
"ternary": "ternary",
"coalesce": "coalesce",
}
)

Expand All @@ -41,6 +38,19 @@ func init() {
for key, name := range sprigFunctions {
liquidEngine.RegisterFilter(name, fncs[key])
}
liquidEngine.RegisterFilter("indent", indent)
liquidEngine.RegisterFilter("nindent", nindent)
liquidEngine.RegisterFilter("replace", strings.ReplaceAll)

liquidEngine.RegisterFilter("default", func(a, b interface{}) interface{} {
fun := fncs["default"].(func(interface{}, interface{}) interface{})
return fun(b, a)
})

liquidEngine.RegisterFilter("ternary", func(a, b, c interface{}) interface{} {
fun := fncs["ternary"].(func(interface{}, interface{}, interface{}) interface{})
return fun(c, a, b)
})
}

type raw struct {
Expand Down
11 changes: 11 additions & 0 deletions pkg/manifests/template/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package template

import (
"strings"

console "github.com/pluralsh/console-client-go"
)

Expand All @@ -20,3 +22,12 @@ func contexts(svc *console.GetServiceDeploymentForAgent_ServiceDeployment) map[s
}
return res
}

func indent(v string, spaces int) string {
pad := strings.Repeat(" ", spaces)
return pad + strings.ReplaceAll(v, "\n", "\n"+pad)
}

func nindent(v string, spaces int) string {
return "\n" + indent(v, spaces)
}

0 comments on commit eb29e2c

Please sign in to comment.