diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8ad98f6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +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 . + +FROM alpine +COPY --from=builder /go/src/github.com/tsg/gotpl/gotpl /usr/local/bin/gotpl +ENTRYPOINT ["/usr/local/bin/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)