bashbrew icon indicating copy to clipboard operation
bashbrew copied to clipboard

Consider using sprig

Open tianon opened this issue 5 years ago • 1 comments

We should spend more time looking at https://masterminds.github.io/sprig/ and determine whether/how much of https://github.com/docker-library/bashbrew/blob/8e42901deafd7baaf313ad4b655f85507b001746/pkg/templatelib/lib.go we could get rid of by using that instead (since it implements many of the same functions like first, last, ternary, etc - some just with a different name like toJson vs our json).

tianon avatar Dec 22 '20 00:12 tianon

With the following very naive implementation, there's a ~1MB increase in binary size (from ~16M up to ~17M).

We'll need to define json as an alias for toJson and getenv will be our own custom wrapper, but otherwise this is basically a drop-in replacement from what I can tell. :sweat_smile:

diff --git a/cmd/bashbrew/cmd-cat.go b/cmd/bashbrew/cmd-cat.go
index 8edb581..46777a3 100644
--- a/cmd/bashbrew/cmd-cat.go
+++ b/cmd/bashbrew/cmd-cat.go
@@ -9,7 +9,9 @@ import (
 
 	"github.com/urfave/cli"
 	"github.com/docker-library/bashbrew/manifest"
-	"github.com/docker-library/bashbrew/pkg/templatelib"
+	//"github.com/docker-library/bashbrew/pkg/templatelib"
+
+	"github.com/Masterminds/sprig/v3"
 )
 
 var DefaultCatFormat = `
@@ -48,7 +50,7 @@ func cmdCat(c *cli.Context) error {
 	}
 
 	var i int
-	tmpl, err := template.New(templateName).Funcs(templatelib.FuncMap).Funcs(template.FuncMap{
+	tmpl, err := template.New(templateName).Funcs(sprig.TxtFuncMap()).Funcs(template.FuncMap{
 		"i": func() int {
 			return i
 		},
diff --git a/go.mod b/go.mod
index a03278e..f3d2c3b 100644
--- a/go.mod
+++ b/go.mod
@@ -3,6 +3,7 @@ module github.com/docker-library/bashbrew
 go 1.13
 
 require (
+	github.com/Masterminds/sprig/v3 v3.2.0
 	github.com/containerd/containerd v1.4.0
 	github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
 	github.com/go-git/go-git/v5 v5.1.0

tianon avatar Dec 22 '20 00:12 tianon