Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slim-sprig functions to template language #451

Merged
merged 2 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:
- kvm
test:
- { name: "recipes", case: "recipes" }
- { name: "escaping", case: "escaping", variables: " -t escaped:\\$ba\\'d\\$gers\\ snakes" }
- { name: "templating", case: "templating", variables: " -t escaped:\\$ba\\'d\\$gers\\ snakes" }
- { name: "debian (amd64)", case: "debian", variables: "-t architecture:amd64" }
- { name: "debian (arm64)", case: "debian", variables: "-t architecture:arm64" }
- { name: "debian (armhf)", case: "debian", variables: "-t architecture:armhf" }
Expand Down
7 changes: 6 additions & 1 deletion actions/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ Comments are allowed and should be prefixed with '#' symbol.
property2: {{$Var}}


The following custom template functions are available
The following custom template functions are available:

- sector: Returns the argument * 512 (convential sector size) e.g. `{{ sector 64 }}`
- escape: Shell escape the argument `{{ escape $var }}`
- functions from [slim-sprig](https://go-task.github.io/slim-sprig/)

Mandatory properties for recipe:

Expand Down Expand Up @@ -81,6 +82,7 @@ import (
"github.com/go-debos/debos"
"gopkg.in/yaml.v2"
"github.com/alessio/shellescape"
"github.com/go-task/slim-sprig/v3"
"path"
"text/template"
"log"
Expand Down Expand Up @@ -246,6 +248,9 @@ func (r *Recipe) Parse(file string, printRecipe bool, dump bool, templateVars ..
}
t.Funcs(funcs)

/* Add slim-sprig functions to template language */
t.Funcs(sprig.FuncMap())

if _, err := t.ParseFiles(file); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ require (
github.com/alessio/shellescape v1.4.2
github.com/docker/go-units v0.5.0
github.com/go-debos/fakemachine v0.0.8
github.com/go-task/slim-sprig/v3 v3.0.0
github.com/google/uuid v1.4.0
github.com/jessevdk/go-flags v1.5.0
github.com/sjoerdsimons/ostree-go v0.0.0-20201014091107-8fae757256f8
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.4
gopkg.in/freddierice/go-losetup.v1 v1.0.0-20170407175016-fc9adea44124
gopkg.in/yaml.v2 v2.4.0
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/go-debos/fakemachine v0.0.8 h1:qCQLQ+dKLQE4ZMbJLMP+vnSrNiFuIBJjP5pnbLT4xpg=
github.com/go-debos/fakemachine v0.0.8/go.mod h1:65Ym4PfsS5ayO0trZ8UoJZFriIBLqbwjrcMKqoaUQoQ=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
Expand All @@ -25,8 +27,8 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/surma/gocpio v1.1.0 h1:RUWT+VqJ8GSodSv7Oh5xjIxy7r24CV1YvothHFfPxcQ=
github.com/surma/gocpio v1.1.0/go.mod h1:zaLNaN+EDnfSnNdWPJJf9OZxWF817w5dt8JNzF9LCVI=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
Expand Down
20 changes: 0 additions & 20 deletions tests/escaping/test.yaml

This file was deleted.

37 changes: 37 additions & 0 deletions tests/templating/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# expected to be run with -t escaped:\$bad\'ge\$rs or similar to verify passing
# variables onwards
{{ $escaped := or .escaped "$mu'room$" }}
architecture: amd64

actions:
- action: run
description: test escaping in command
chroot: false
command: echo {{ escape $escaped }} > test.a
- action: run
description: escaping via heredoc
chroot: false
command: |-
cat << 'EOF' > test.b
{{ $escaped }}
EOF
- action: run
description: validate escaping
chroot: false
command: diff -u test.a test.b
- action: run
description: sector custom function
chroot: false
command: test 4096 -eq {{ sector 8 }}
- action: run
description: sprig string functions
chroot: false
command: test "HELLOHELLO" = {{ "hello" | upper | repeat 2 }}
- action: run
description: sprig integer math functions
chroot: false
command: test 42 -eq {{ mul 4 10 | add 2 }}
- action: run
description: sprig conversion function
chroot: false
command: test 42 -eq {{ 0x2a | int }}