-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtpl.go
48 lines (41 loc) · 1.01 KB
/
tpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"text/template"
"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...)
if err != nil {
return fmt.Errorf("Error parsing template(s): %v", err)
}
buf := bytes.NewBuffer(nil)
_, err = io.Copy(buf, values_in)
if err != nil {
return fmt.Errorf("Failed to read standard input: %v", err)
}
var values map[string]interface{}
err = yaml.Unmarshal(buf.Bytes(), &values)
if err != nil {
return fmt.Errorf("Failed to parse standard input: %v", err)
}
err = tpl.Execute(out, values)
if err != nil {
return fmt.Errorf("Failed to parse standard input: %v", err)
}
return nil
}
func main() {
err := ExecuteTemplates(os.Stdin, os.Stdout, os.Args[1:]...)
if err != nil {
log.Println(err)
os.Exit(1)
}
}