Skip to content

Commit

Permalink
Replace custom templating function by a light version of Sprig, for a…
Browse files Browse the repository at this point in the history
… more powerful templating engine in jobfiles.

cf. https://masterminds.github.io/sprig/ for Sprig documentation
cf. https://github.com/go-task/slim-sprig/ for the fork repository
  • Loading branch information
gnutix committed Nov 17, 2023
1 parent 29627fa commit 7234651
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 23 deletions.
28 changes: 6 additions & 22 deletions client/jobfile/reader.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package jobfile

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
Expand All @@ -14,6 +12,9 @@ import (
"time"

"github.com/gammadia/alfred/proto"
// Fork of github.com/Masterminds/sprig/v3 removing functions having 3rd party dependencies. List available here :
// https://github.com/go-task/slim-sprig/commit/4aa88173255771335225fa85e97341262276d42b?w=1#diff-5faaf3bff8320883fedc2b5e1828c8cea82467bd7d8357e1c824dafe50835bfb
"github.com/go-task/slim-sprig/v3"
"github.com/samber/lo"
"google.golang.org/protobuf/types/known/durationpb"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -128,10 +129,9 @@ type TemplateData struct {

func evaluateTemplate(source string, dir string, options ReadOptions) (string, error) {
var userError error
tmpl, err := template.New("jobfile").Funcs(template.FuncMap{
"base64": func(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
},

tmpl, err := template.New("jobfile").Funcs(sprig.FuncMap()).Funcs(template.FuncMap{
// This one is better than sprig's fail() because it produces less noise in the error message.
"error": func(err string) any {
userError = errors.New(err)
panic(nil)
Expand All @@ -148,16 +148,6 @@ func evaluateTemplate(source string, dir string, options ReadOptions) (string, e
return strings.TrimRight(string(output), "\n\r")
}
},
"join": func(sep string, s []string) string {
return strings.Join(s, sep)
},
"json": func(v any) (string, error) {
buf, err := json.Marshal(v)
return string(buf), err
},
"lines": func(s string) []string {
return strings.Split(s, "\n")
},
"shell": func(script string) string {
cmd := exec.Command("/bin/bash", "-euo", "pipefail", "-c", script)
cmd.Dir = dir
Expand All @@ -170,12 +160,6 @@ func evaluateTemplate(source string, dir string, options ReadOptions) (string, e
return strings.TrimRight(string(output), "\n\r")
}
},
"split": func(sep string, s string) []string {
return strings.Split(s, sep)
},
"trim": func(s string) string {
return strings.TrimSpace(s)
},
}).Parse(source)
if err != nil {
return "", fmt.Errorf("failed to parse template: %w", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/briandowns/spinner v1.23.0
github.com/docker/docker v24.0.7+incompatible
github.com/fatih/color v1.15.0
github.com/go-task/slim-sprig/v3 v3.0.0
github.com/gophercloud/gophercloud v1.7.0
github.com/rivo/tview v0.0.0-20231102183219-1b91b8131c43
github.com/rivo/uniseg v0.4.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
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/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down
2 changes: 1 addition & 1 deletion hack/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ steps:
- dockerfile: "job.dockerfile"
context: "."
tasks:
{{range (lines (shell "sloth instance --app tipee list -o json | jq -r '.[] | select(.features.tests) | .id' | tail -n 1"))}}
{{range (splitList "\n" (shell "sloth instance --app tipee list -o json | jq -r '.[] | select(.features.tests) | .id' | tail -n 5"))}}
- {{.}}
{{end}}

0 comments on commit 7234651

Please sign in to comment.