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

Consider using sprig #22

Open
tianon opened this issue Dec 22, 2020 · 1 comment · May be fixed by #23
Open

Consider using sprig #22

tianon opened this issue Dec 22, 2020 · 1 comment · May be fixed by #23

Comments

@tianon
Copy link
Member

tianon commented Dec 22, 2020

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
Copy link
Member Author

tianon commented Dec 22, 2020

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. 😅

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant