-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (98 loc) · 2.21 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2020 Serge Léger. All rights reserved.
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"text/template"
)
var templateFuncs = template.FuncMap{
// string helper methods
"stringsReplace": strings.Replace,
"tablecell": mdTableCell,
"join": join,
// execute
"execute": executeSafe,
}
func main() {
if err := mainErr(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
func mainErr(args []string) error {
var flags Flags
if err := flags.Parse(args); err != nil {
return err
}
tmpl, err := readTemplate(flags.Template)
if err != nil {
return err
}
// Read JSON source(s) data in memory
data := make([]JSON, len(flags.Args))
for i, f := range flags.Args {
data[i].Filename = f
r, err := openFile(f)
if err != nil {
return err
}
err = NewJSONReader(&data[i], flags.JSONLines).ReadFrom(r)
r.Close()
if err != nil {
return err
}
}
// simplify the template object if we were called with a single input file.
var tmplObj interface{} = data
if len(data) == 1 {
tmplObj = data[0]
}
// Buffer the template's output
var buf bytes.Buffer
err = tmpl.Execute(&buf, tmplObj)
if err != nil {
return err
}
// Copy results to stdout
w, err := createFile(flags.Output)
if err != nil {
return err
}
_, err = io.Copy(w, &buf)
return w.Close()
}
// readTemplates reads and creates the template object.
func readTemplate(file string) (*template.Template, error) {
r, err := openFile(file)
if err != nil {
return nil, err
}
defer r.Close()
content, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
tmpl := template.New(file)
tmpl.Funcs(templateFuncs)
return tmpl.Parse(string(content))
}
// createFile opens the file for writing, if the file is "-" returns stdout.
func createFile(file string) (io.WriteCloser, error) {
if file == "-" {
return os.Stdout, nil
}
return os.Create(file)
}
// openFile opens a file for reading, if the file is "-" returns stdin.
func openFile(file string) (io.ReadCloser, error) {
if file == "-" {
return ioutil.NopCloser(os.Stdin), nil
}
return os.Open(file)
}