From 423feb83aaa7ce29f2034b3e2d5098e1f926355d Mon Sep 17 00:00:00 2001 From: Filip Pytloun Date: Thu, 29 Mar 2018 10:27:54 +0200 Subject: [PATCH 1/2] Add Dockerfile --- Dockerfile | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..48ddb7c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM golang:1.10 as builder +WORKDIR /go/src/github.com/tsg/gotpl +RUN go get -d -v gopkg.in/yaml.v2 +COPY tpl.go . +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix gotpl -o gotpl . + +FROM alpine +COPY --from=builder /go/src/github.com/tsg/gotpl/gotpl /usr/local/bin/gotpl +ENTRYPOINT ["/usr/local/bin/gotpl"] From 98d6edb54500184b43779a2b27a31eb6a3011eb5 Mon Sep 17 00:00:00 2001 From: Filip Pytloun Date: Mon, 17 Dec 2018 12:35:09 +0100 Subject: [PATCH 2/2] Use sprig functions --- Dockerfile | 1 + tpl.go | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 48ddb7c..8ad98f6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ FROM golang:1.10 as builder WORKDIR /go/src/github.com/tsg/gotpl RUN go get -d -v gopkg.in/yaml.v2 +RUN go get -d -v github.com/Masterminds/sprig COPY tpl.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix gotpl -o gotpl . diff --git a/tpl.go b/tpl.go index 563d847..4c06231 100644 --- a/tpl.go +++ b/tpl.go @@ -4,18 +4,30 @@ import ( "bytes" "fmt" "io" + "io/ioutil" "log" "os" "text/template" + "github.com/Masterminds/sprig" "gopkg.in/yaml.v2" ) // Reads a YAML document from the values_in stream, uses it as values // for the tpl_files templates and writes the executed templates to // the out stream. -func ExecuteTemplates(values_in io.Reader, out io.Writer, tpl_files ...string) error { - tpl, err := template.ParseFiles(tpl_files...) +func ExecuteTemplates(values_in io.Reader, out io.Writer, tpl_files string) error { + var r io.Reader + r, err := os.Open(tpl_files) + if err != nil { + return err + } + templateData, err := ioutil.ReadAll(r) + if err != nil { + return fmt.Errorf("Could not read input: %v", err) + } + + tpl, err := template.New("base").Funcs(sprig.TxtFuncMap()).Parse(string(templateData)) if err != nil { return fmt.Errorf("Error parsing template(s): %v", err) } @@ -40,7 +52,7 @@ func ExecuteTemplates(values_in io.Reader, out io.Writer, tpl_files ...string) e } func main() { - err := ExecuteTemplates(os.Stdin, os.Stdout, os.Args[1:]...) + err := ExecuteTemplates(os.Stdin, os.Stdout, os.Args[1]) if err != nil { log.Println(err) os.Exit(1)