Skip to content

Commit

Permalink
Merge pull request #28 from davidovich/23-use-sprig-template-lib
Browse files Browse the repository at this point in the history
add sprig templating for richer templates
  • Loading branch information
davidovich authored Apr 12, 2019
2 parents e78e0b0 + 8a1b471 commit a3efe3a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
module github.com/davidovich/summon

require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.4.2 // indirect
github.com/Masterminds/sprig v2.18.0+incompatible
github.com/gobuffalo/packr/v2 v2.0.10-0.20190331154738-11df0b070aa0
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.2.0 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lithammer/dedent v1.1.0
github.com/pkg/errors v0.8.1
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/sprig v2.18.0+incompatible h1:QoGhlbC6pter1jxKnjMFxT8EqsLuDE6FEcNbWEpw+lI=
github.com/Masterminds/sprig v2.18.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -26,6 +32,12 @@ github.com/gobuffalo/packr/v2 v2.0.10-0.20190331154738-11df0b070aa0 h1:GBRbBt01w
github.com/gobuffalo/packr/v2 v2.0.10-0.20190331154738-11df0b070aa0/go.mod h1:pcdZvfinlUBkDIFERw5fspLkUwwjq4OCekeXTCjtruM=
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754 h1:tpom+2CJmpzAWj5/VEHync2rJGi+epHNIeRSWjzGA+4=
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
Expand Down
6 changes: 3 additions & 3 deletions pkg/summon/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type options struct {

args []string

data *map[string]interface{}
data map[string]interface{}
}

// Option allows specifying configuration settings
Expand Down Expand Up @@ -80,7 +80,7 @@ func ShowTree(tree bool) Option {
func JSON(j string) Option {
return func(opts *options) error {
if j == "" {
opts.data = nil
opts.data = map[string]interface{}{}
return nil
}

Expand All @@ -90,7 +90,7 @@ func JSON(j string) Option {
if err != nil {
return err
}
opts.data = &data
opts.data = data
return nil
}
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/summon/summon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"

"github.com/Masterminds/sprig"
"github.com/davidovich/summon/internal/testutil"
"github.com/gobuffalo/packr/v2/file"
"github.com/spf13/afero"
Expand Down Expand Up @@ -59,11 +60,8 @@ func (s *Summoner) Summon(opts ...Option) (string, error) {
return s.copyOneFile(boxedFile, "")
}

func renderTemplate(tmpl string, data *map[string]interface{}) (string, error) {
if data == nil {
return tmpl, nil
}
t, err := template.New("Summon").Parse(tmpl)
func renderTemplate(tmpl string, data map[string]interface{}) (string, error) {
t, err := template.New("Summon").Funcs(sprig.FuncMap()).Parse(tmpl)
if err != nil {
return tmpl, err
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/summon/summon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,16 @@ func TestSummonScenarios(t *testing.T) {
expectedContent: "hello World!",
},
{
desc: "no rendering",
desc: "no data",
filename: "template.file",
expectedFileName: "overridden_dir/template.file",
expectedContent: "hello {{ .Name }}",
expectedContent: "hello ",
},
{
desc: "sprig rendering",
filename: "sprigcontent.gotmpl",
expectedFileName: "overridden_dir/sprigcontent.gotmpl",
expectedContent: "HELLO\n25",
},
{
desc: "alias",
Expand Down
2 changes: 2 additions & 0 deletions pkg/summon/testdata/sprigcontent.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ upper "hello" }}
{{ mul 5 5}}

0 comments on commit a3efe3a

Please sign in to comment.