-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht3.go
55 lines (46 loc) · 1.11 KB
/
t3.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
49
50
51
52
53
54
55
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/hoisie/mustache"
)
var (
dataPath = flag.String("d", "", "json data file path")
templatePath = flag.String("t", "", "mustache template file path")
)
func main() {
var data interface{}
var jsonFile io.ReadCloser
var err error
var rawJSON []byte
var templateFile io.ReadCloser
var template []byte
// templatePath := "template2.mustache"
// dataPath := "data2.json"
flag.Parse()
if templateFile, err = os.Open(*templatePath); err != nil {
fmt.Println("Could not open template file")
os.Exit(1)
}
defer templateFile.Close()
if template, err = ioutil.ReadAll(templateFile); err != nil {
fmt.Println("Could not read template file")
os.Exit(1)
}
if jsonFile, err = os.Open(*dataPath); err != nil {
fmt.Println("Could not open data file")
os.Exit(1)
}
defer jsonFile.Close()
if rawJSON, err = ioutil.ReadAll(jsonFile); err != nil {
fmt.Println("Could not read data file")
os.Exit(1)
}
json.Unmarshal([]byte(rawJSON), &data)
rendered := mustache.Render(string(template), data)
fmt.Printf(rendered)
}