Skip to content

Commit

Permalink
Add sprig template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsauter committed Nov 3, 2023
1 parent 75ea23c commit f35a700
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 13 deletions.
3 changes: 1 addition & 2 deletions build/docs/render.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ The asciidoc template can access the value of the field `a` by referencing `{{.o

Note that only JSON and YAML formats are recognized as such. If a matching file does not end in either `.json` or `.y(a)ml`, its entire content is made available under the key `value`. For example, the glob pattern `*.log` might match the file `pipeline-run.log`, which would expose the content of the file as `pipeline_run.value` to the template.

The Go template has access to the following helper functions:
The Go template can make use of template functions provided by link:http://masterminds.github.io/sprig/[sprig], as well as the following helper functions:

* `fromMultiYAML`. Turns a string of multiple YAML documents (separated with `---`) into a slice of maps.
* `toYAML`. Turns the given object into a YAML string.
* `parseTime`. Parses a string using the specified layout into a `time.Time`. This function is just a wrapper around link:https://pkg.go.dev/time#Parse[time.Parse], see its documentation for details how to specify various layouts. Once parsed, the time can be formatted with `{{$t.Format "<layout>"}}` as per link:https://pkg.go.dev/time#Time.Format[time.Format].
* `toSentence`. Turns a slice into a string enumerating its items. The words are connected with commas, except for the last two words, which are connected with "and".
* `keys`. Returns a slice of all keys of given map.
Expand Down
3 changes: 2 additions & 1 deletion cmd/render/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"io"
"strings"

"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func (d *plainDecoder) Decode(v any) error {
}
if x, ok := v.(*map[string]interface{}); ok {
z := *x
z["value"] = string(b)
z["value"] = strings.TrimSpace(string(b))
} else {
return errors.New("unexpected type")
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"regexp"
"strings"
"text/template"

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

var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9]+`)
Expand Down Expand Up @@ -43,6 +45,7 @@ func render(baseDir, templateGlob, outputDir string, dataSourceGlobs []string) e
tmpl, err := template.
New(templateBase).
Funcs(templateFuncs).
Funcs(sprig.FuncMap()).
ParseFiles(templateFile)
if err != nil {
return fmt.Errorf("parse template %q: %s", templateFile, err)
Expand Down
7 changes: 0 additions & 7 deletions cmd/render/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package main
import (
"html/template"
"strings"
"time"

"sigs.k8s.io/yaml"
)

var templateFuncs = template.FuncMap{
"fromMultiYAML": fromMultiYAML,
"toYAML": toYAML,
"parseTime": parseTime,
"toSentence": toSentence,
"keys": keys,
}
Expand All @@ -38,11 +36,6 @@ func toYAML(unmarshalled any) (string, error) {
return string(b), err
}

// parseTime parses a string using the specified layout into a time.Time.
func parseTime(layout, t string) (time.Time, error) {
return time.Parse(layout, t)
}

// toSentence turns a slice into a string enumerating its items.
// The words are connected with commas, except for the last two words,
// which are connected with "and".
Expand Down
3 changes: 1 addition & 2 deletions docs/render.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ The asciidoc template can access the value of the field `a` by referencing `{{.o

Note that only JSON and YAML formats are recognized as such. If a matching file does not end in either `.json` or `.y(a)ml`, its entire content is made available under the key `value`. For example, the glob pattern `*.log` might match the file `pipeline-run.log`, which would expose the content of the file as `pipeline_run.value` to the template.

The Go template has access to the following helper functions:
The Go template can make use of template functions provided by link:http://masterminds.github.io/sprig/[sprig], as well as the following helper functions:

* `fromMultiYAML`. Turns a string of multiple YAML documents (separated with `---`) into a slice of maps.
* `toYAML`. Turns the given object into a YAML string.
* `parseTime`. Parses a string using the specified layout into a `time.Time`. This function is just a wrapper around link:https://pkg.go.dev/time#Parse[time.Parse], see its documentation for details how to specify various layouts. Once parsed, the time can be formatted with `{{$t.Format "<layout>"}}` as per link:https://pkg.go.dev/time#Time.Format[time.Format].
* `toSentence`. Turns a slice into a string enumerating its items. The words are connected with commas, except for the last two words, which are connected with "and".
* `keys`. Returns a slice of all keys of given map.

Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/opendevstack/ods-pipeline-adoc
go 1.21.0

require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/google/go-cmp v0.5.9
github.com/opendevstack/ods-pipeline v0.14.0
github.com/tektoncd/pipeline v0.50.1
Expand All @@ -12,6 +13,8 @@ require (
require (
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blendle/zapdriver v1.3.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
Expand All @@ -35,11 +38,14 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
Expand All @@ -50,12 +56,15 @@ require (
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/prometheus/statsd_exporter v0.21.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sonatype-nexus-community/gonexus v0.59.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/oauth2 v0.9.0 // indirect
Expand Down
Loading

0 comments on commit f35a700

Please sign in to comment.