-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtp.go
More file actions
52 lines (42 loc) · 1.06 KB
/
Copy pathtp.go
File metadata and controls
52 lines (42 loc) · 1.06 KB
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
49
50
51
52
/* This is a trivial template processor for golang/pkg/text/template. It takes its data as a JSON file specified in the first argument and the template specified in the second argument. Output is to stdout.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"text/template"
)
var (
fLax = flag.Bool("lax", false, "allow missing keys")
)
func usage() {
fmt.Println("usage: tp [-lax] <vars file> <template file>")
os.Exit(-1)
}
func fatalIfError(err error, msg string) {
if err != nil {
log.Fatal("error ", msg, ": ", err)
}
}
func main() {
flag.Parse()
varsFile := flag.Arg(0)
tmplFile := flag.Arg(1)
if varsFile == "" || tmplFile == "" {
usage()
}
varsData, err := ioutil.ReadFile(varsFile)
fatalIfError(err, "reading vars file")
vars := map[string]interface{}{}
fatalIfError(json.Unmarshal(varsData, &vars), "parsing vars JSON")
t, err := template.ParseFiles(tmplFile)
fatalIfError(err, "parsing template file")
if !*fLax {
t.Option("missingkey=error")
}
fatalIfError(t.Execute(os.Stdout, vars), "rendering template")
}