Skip to content

Commit

Permalink
ref!: upgrade to sprout 0.5.1, template behavior has changed
Browse files Browse the repository at this point in the history
As sprout has adopted it's new registry system, this refactors the
template layer to use the new handler system. For consistency, the `env`
registry is excluded for security reasons as was previously done, and
all other registries (except the `backward` compat registry) are enabled
and built into the templating engine in mango.

As sprout has also adopted it's updated/improved versions of templating
functions, there are also a few changes in behavior that required test
changes and were noticed, but there may be others. In particular:

- `replace` replaces _all_ occurences by default now, rather than just
  the first
- `regexReplaceAll` better handles regexs it seems and made me notice
  the regex in the test template wasn't really doing what I intended, so
there's that as well
  • Loading branch information
tjhop committed Aug 28, 2024
1 parent 1ce4eeb commit 9237aed
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.0
require (
github.com/dominikbraun/graph v0.23.0
github.com/dustin/go-humanize v1.0.1
github.com/go-sprout/sprout v0.4.1
github.com/go-sprout/sprout v0.5.1
github.com/gobwas/glob v0.2.3
github.com/hashicorp/go-sockaddr v1.0.6
github.com/moby/moby v27.1.1+incompatible
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-sprout/sprout v0.4.1 h1:grvsR21YepGs64EFoIXg4g+5OzIZFwmsw5Y88Wod9sI=
github.com/go-sprout/sprout v0.4.1/go.mod h1:jRgO0n+24zLgiPAg/6rMaeq2oEnBSGlZiHUoK3hnQc4=
github.com/go-sprout/sprout v0.5.1 h1:/9hcxDzq7YOWcOvWMW4Ghob5hw6CWoD3sVuk3FDGOgQ=
github.com/go-sprout/sprout v0.5.1/go.mod h1:P6ETppcGn1BR0HZ8r+660aP2hJH7xiamIGiWjA+AE4o=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down Expand Up @@ -132,6 +132,7 @@ github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
56 changes: 39 additions & 17 deletions internal/manager/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@ import (
"context"
"fmt"
"path/filepath"
"sync"
"text/template"

"github.com/go-sprout/sprout"
"github.com/go-sprout/sprout/registry/checksum"
"github.com/go-sprout/sprout/registry/conversion"
"github.com/go-sprout/sprout/registry/crypto"
"github.com/go-sprout/sprout/registry/encoding"
"github.com/go-sprout/sprout/registry/filesystem"
"github.com/go-sprout/sprout/registry/maps"
"github.com/go-sprout/sprout/registry/numeric"
"github.com/go-sprout/sprout/registry/random"
"github.com/go-sprout/sprout/registry/reflect"
"github.com/go-sprout/sprout/registry/regexp"
"github.com/go-sprout/sprout/registry/semver"
"github.com/go-sprout/sprout/registry/slices"
"github.com/go-sprout/sprout/registry/std"
"github.com/go-sprout/sprout/registry/strings"
"github.com/go-sprout/sprout/registry/time"
"github.com/go-sprout/sprout/registry/uniqueid"
socktmpl "github.com/hashicorp/go-sockaddr/template"
"github.com/oklog/ulid/v2"

"github.com/tjhop/mango/internal/shell"
)

var (
once sync.Once
sproutFuncMap template.FuncMap
sproutDisabledFuncs = []string{"env", "expandenv"}
)

type VariableSlice = shell.VariableSlice
type VariableMap = shell.VariableMap

Expand Down Expand Up @@ -49,29 +58,42 @@ type templateData struct {
Storage storageMetadata
}

func init() {
once.Do(func() {
sproutFuncMap = sprout.TxtFuncMap()
for _, f := range sproutDisabledFuncs {
delete(sproutFuncMap, f)
}
})
}

func templateScript(ctx context.Context, path string, view templateView, funcMap template.FuncMap, invDefinedTemplates ...string) (string, error) {
var (
buf bytes.Buffer
err error
)

handler := sprout.New()
if err := handler.AddRegistries(
checksum.NewRegistry(),
conversion.NewRegistry(),
crypto.NewRegistry(),
encoding.NewRegistry(),
filesystem.NewRegistry(),
maps.NewRegistry(),
numeric.NewRegistry(),
random.NewRegistry(),
reflect.NewRegistry(),
regexp.NewRegistry(),
semver.NewRegistry(),
slices.NewRegistry(),
std.NewRegistry(),
strings.NewRegistry(),
time.NewRegistry(),
uniqueid.NewRegistry(),
); err != nil {
return "", fmt.Errorf("Failed to add sprout registries to handler: %s\n", err.Error())
}

// init template and funcs
t := template.New(filepath.Base(path)).
Funcs(funcMap).
Funcs(socktmpl.SourceFuncs).
Funcs(socktmpl.SortFuncs).
Funcs(socktmpl.FilterFuncs).
Funcs(socktmpl.HelperFuncs).
Funcs(sproutFuncMap)
Funcs(handler.Build())

if len(invDefinedTemplates) > 0 {
if t, err = t.ParseFiles(invDefinedTemplates...); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/mockup/inventory/modules/test-template/apply
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ Mount info:
{{- end }}
-------------------
Testing template functions:
String replacement (input 'foo foo foo', expected output 'bar foo foo'): {{ "foo foo foo" | replace "foo" "bar" }}
Contrived regex replacement (input 'a bc d', expected output: 'abcd'): {{ mustRegexReplaceAll "([A-Za-z]+).+([A-Za-z]+).+([A-Za-z]+)" "a bc d" "$1$2$3" }}
String replacement (input 'foo foo foo', expected output 'bar bar bar'): {{ "foo foo foo" | replace "foo" "bar" }}
Contrived regex replacement (input 'a bc d', expected output: 'abcd'): {{ mustRegexReplaceAll "([A-Za-z]+) +([A-Za-z]+) +([A-Za-z]+)" "a bc d" "$1$2$3" }}
Private IP: {{ GetPrivateIP }}
Public IP: {{ GetPublicIP }}
Interfaces: {{ GetAllInterfaces }}
Expand Down

0 comments on commit 9237aed

Please sign in to comment.