-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (162 loc) · 4.22 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// gotmpl is a simple command line tool that will substitute the
// variables from a data file into a Go text template file.
//
// Get it:
// go get -u github.com/msample/gotmpl
//
// Use it:
// gotmpl -d dat.yml cfg.txt.tmpl > cfg.txt
//
// cat dat.yml | gotmpl cfg.txt.tmpl > cfg.txt
//
// cat cfg.txt.tmpl | gotmpl -d data.json > cfg.txt
//
// gotmpl -d dat.yml cfg.txt.tmpl cfg.txt.tmpl2 > cfg.txt
//
// gotmpl -logtostderr -d dat.yml cfg.txt.tmpl > cfg.txt
//
// gotmpl -h
//
//
// Data file may contain YAML, JSON, HCL or TOML. Gotmpl tries the
// parsers in that order and takes the result of the first one that
// doesn't complain.
//
// Use -logtostderr option if having problems. Template syntax defined
// here: https://godoc.org/text/template
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"text/template"
"github.com/golang/glog"
"github.com/hashicorp/hcl"
toml "github.com/pelletier/go-toml"
"gopkg.in/yaml.v2"
)
var (
varsFile = flag.String("d", "-", "YAML, JSON, HCL or TOML file with var values to substitute into the template. Use '-' for stdin (default).")
)
func Usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] [tmplateFile...]\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
flag.Usage = Usage
flag.Parse()
defer glog.Flush()
if len(flag.Args()) == 0 && *varsFile == "-" {
fmt.Fprintf(os.Stderr, "Cannot read both template and data from stdin\n")
Usage()
os.Exit(1)
}
var tmpl *template.Template
var data map[string]interface{}
var err error
// read stdin last so fail fast&first file-based info
if *varsFile == "-" {
tmpl = readTemplates()
data = readData()
} else {
data = readData()
tmpl = readTemplates()
}
glog.Infof("Data is %v\n", data)
err = tmpl.Execute(os.Stdout, data)
if err != nil {
glog.Errorf("Template execution error: %v\n", err)
os.Exit(2)
}
}
func readData() map[string]interface{} {
data, err := parseVars(*varsFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading vars data: %v\n", err)
os.Exit(3)
}
return data
}
func readTemplates() *template.Template {
// files
if len(flag.Args()) > 0 {
tmpl, err := template.ParseFiles(flag.Args()...)
if err != nil {
glog.Errorf("Template parsing error: %v\n", err)
os.Exit(4)
}
return tmpl
}
// stdin
s, err := ioutil.ReadAll(os.Stdin)
if err != nil {
glog.Errorf("Error reading stdin: %v\n", err)
os.Exit(5)
}
tmpl, err := template.New("t1").Parse(string(s))
if err != nil {
glog.Errorf("Template parsing error: %v\n", err)
os.Exit(6)
}
return tmpl
}
// parseVars tries to parse the input and returns the result of the
// first successful parse in this order: YAML, JSON, HCL & TOML.
// File value of "-" will read stdin until closed and then parse.
func parseVars(file string) (map[string]interface{}, error) {
// adaptive variant of parse code in github.com/spf13/viper (MIT)
// minus properties file support
f := os.Stdin
var err error
if file != "-" {
f, err = os.Open(file)
if err != nil {
return nil, err
}
}
v, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
pv := make(map[string]interface{})
err = yaml.Unmarshal(v, &pv)
if err == nil {
glog.Info("Yaml detected")
return pv, nil
}
glog.Infof("YAML err: %v", err)
pv = make(map[string]interface{})
err = json.Unmarshal(v, &pv)
if err == nil {
// yaml should cover JSON but in case...
glog.Info("JSON detected")
return pv, nil
}
glog.Infof("JSON err: %v", err)
pv = make(map[string]interface{})
o, err1 := hcl.Parse(string(v))
var err2 error
if err1 == nil {
err2 = hcl.DecodeObject(&pv, o)
}
if err1 == nil && err2 == nil {
glog.Info("HCL detected")
return pv, nil
}
glog.Infof("HCL errs: %v, %v", err1, err2)
pv = make(map[string]interface{})
t, err := toml.LoadReader(bytes.NewBuffer(v))
if err == nil {
tm := t.ToMap()
for k, v := range tm {
pv[k] = v
}
glog.Info("TOML detected")
return pv, nil
}
glog.Infof("TOML err: %v", err)
return nil, fmt.Errorf("data in '%v' failed to parse as YAML, JSON, HCL or TOML", file)
}