Skip to content

Commit eee87c5

Browse files
committed
snapshot
2 parents d1cb3ab + 3dbd35e commit eee87c5

File tree

11 files changed

+192
-79
lines changed

11 files changed

+192
-79
lines changed

cmds/jsoncols/jsoncols.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ Would yield
9090
permissive bool
9191
)
9292

93+
func handleError(err error, exitCode int) {
94+
if permissive == false {
95+
fmt.Fprintf(os.Stderr, "%s\n", err)
96+
}
97+
if exitCode >= 0 {
98+
os.Exit(exitCode)
99+
}
100+
}
101+
93102
func init() {
94103
// Basic Options
95104
flag.BoolVar(&showHelp, "h", false, "display help")
@@ -106,6 +115,7 @@ func init() {
106115
flag.BoolVar(&runInteractive, "repl", false, "run interactively")
107116
flag.StringVar(&delimiter, "d", delimiter, "set the delimiter for multi-field output")
108117
flag.BoolVar(&permissive, "permissive", false, "suppress error messages")
118+
flag.BoolVar(&permissive, "quiet", false, "suppress error messages")
109119
}
110120

111121
func main() {
@@ -139,15 +149,13 @@ func main() {
139149

140150
in, err := cli.Open(inputFName, os.Stdin)
141151
if err != nil {
142-
fmt.Fprintf(os.Stderr, "%s\n", err)
143-
os.Exit(1)
152+
handleError(err, 1)
144153
}
145154
defer cli.CloseFile(inputFName, in)
146155

147156
out, err := cli.Create(outputFName, os.Stdout)
148157
if err != nil {
149-
fmt.Fprintf(os.Stderr, "%s\n", err)
150-
os.Exit(1)
158+
handleError(err, 1)
151159
}
152160
defer cli.CloseFile(outputFName, out)
153161

@@ -166,14 +174,12 @@ func main() {
166174
// READ in the JSON document
167175
buf, err := ioutil.ReadAll(in)
168176
if err != nil {
169-
fmt.Fprintf(os.Stderr, "%s\n", err)
170-
os.Exit(1)
177+
handleError(err, 1)
171178
}
172179
// JSON Decode our document
173180
data, err := dotpath.JSONDecode(buf)
174181
if err != nil {
175-
fmt.Fprintf(os.Stderr, "%s\n", err)
176-
os.Exit(1)
182+
handleError(err, 1)
177183
}
178184

179185
// For each dotpath expression return a result
@@ -194,14 +200,12 @@ func main() {
194200
default:
195201
src, err := json.Marshal(result)
196202
if err != nil {
197-
fmt.Fprintf(os.Stderr, "%s\n", err)
198-
os.Exit(1)
203+
handleError(err, 1)
199204
}
200205
fmt.Fprintf(out, "%s", src)
201206
}
202-
} else if permissive == false {
203-
fmt.Fprintf(os.Stderr, "%s\n", err)
204-
os.Exit(1)
207+
} else {
208+
handleError(err, 1)
205209
}
206210
}
207211
}

cmds/jsonrange/jsonrange.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ func srcVals(data interface{}, limit int) ([]string, error) {
226226
return nil, fmt.Errorf("%T does not support for range", data)
227227
}
228228

229+
func handleError(err error, exitCode int) {
230+
if permissive == false {
231+
fmt.Fprintf(os.Stderr, "%s\n", err)
232+
}
233+
if exitCode >= 0 {
234+
os.Exit(exitCode)
235+
}
236+
}
237+
229238
func init() {
230239
// Standard Options
231240
flag.BoolVar(&showHelp, "h", false, "display help")
@@ -244,6 +253,7 @@ func init() {
244253
flag.StringVar(&delimiter, "delimiter", "", "set delimiter for range output")
245254
flag.IntVar(&limit, "limit", 0, "limit the number of items output")
246255
flag.BoolVar(&permissive, "permissive", false, "suppress errors messages")
256+
flag.BoolVar(&permissive, "quiet", false, "suppress errors messages")
247257
}
248258

249259
func main() {
@@ -274,15 +284,13 @@ func main() {
274284

275285
in, err := cli.Open(inputFName, os.Stdin)
276286
if err != nil {
277-
fmt.Fprintf(os.Stderr, "%s\n", err)
278-
os.Exit(1)
287+
handleError(err, 1)
279288
}
280289
defer cli.CloseFile(inputFName, in)
281290

282291
out, err := cli.Create(outputFName, os.Stdout)
283292
if err != nil {
284-
fmt.Fprintf(os.Stderr, "%s\n", err)
285-
os.Exit(1)
293+
handleError(err, 1)
286294
}
287295
defer cli.CloseFile(outputFName, out)
288296

@@ -294,16 +302,11 @@ func main() {
294302
// Read in the complete JSON data structure
295303
buf, err := ioutil.ReadAll(in)
296304
if err != nil {
297-
fmt.Fprintf(os.Stderr, "%s\n", err)
298-
os.Exit(1)
305+
handleError(err, 1)
299306
}
300307

301-
if len(buf) == 0 && permissive == false {
302-
fmt.Fprintln(os.Stderr, cfg.Usage())
303-
os.Exit(1)
304-
}
305-
if len(buf) == 0 && permissive == true {
306-
os.Exit(0)
308+
if len(buf) == 0 {
309+
handleError(fmt.Errorf("%s", cfg.Usage()), 1)
307310
}
308311

309312
var (
@@ -323,34 +326,31 @@ func main() {
323326
data, err = dotpath.EvalJSON(p, buf)
324327
}
325328
if err != nil {
326-
fmt.Fprintf(os.Stderr, "%s\n", err)
327-
os.Exit(1)
329+
handleError(err, 1)
328330
}
329331
switch {
330332
case showLength:
331333
if l, err := getLength(data); err == nil {
332334
fmt.Fprintf(out, "%d", l)
333335
} else {
334-
fmt.Fprintf(os.Stderr, "%s\n", err)
336+
handleError(err, -1)
335337
}
336338
case showLast:
337339
if l, err := getLength(data); err == nil {
338340
fmt.Fprintf(out, "%d", l-1)
339341
} else {
340-
fmt.Fprintf(os.Stderr, "%s\n", err)
342+
handleError(err, -1)
341343
}
342344
case showValues:
343345
elems, err := srcVals(data, limit-1)
344346
if err != nil {
345-
fmt.Fprintf(os.Stderr, "%s\n", err)
346-
os.Exit(1)
347+
handleError(err, 1)
347348
}
348349
fmt.Fprintln(out, strings.Join(elems, delimiter))
349350
default:
350351
elems, err := srcKeys(data, limit-1)
351352
if err != nil {
352-
fmt.Fprintf(os.Stderr, "%s\n", err)
353-
os.Exit(1)
353+
handleError(err, 1)
354354
}
355355
fmt.Fprintln(out, strings.Join(elems, delimiter))
356356
}

cmds/vcard2json/vcard2json.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ EXAMPLES
4747
4848
Simple usage of building a CSV file one rows at a time.
4949
50-
cat my.cvf | %s > myVCard.json
50+
cat my.cvf | %s > myVCard.json
5151
5252
Or reading, writing to specific file
5353

cmds/xlsx2csv/xlsx2csv.go

+4-19
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ Putting it all together in a shell script.
7676
// Application Options
7777
showSheetCount bool
7878
showSheetNames bool
79-
verbose bool
80-
permissive bool
8179
)
8280

8381
func sheetCount(workBookName string) (int, error) {
@@ -108,23 +106,12 @@ func xlsx2CSV(out io.Writer, workBookName, sheetName string) error {
108106
results := [][]string{}
109107
cells := []string{}
110108
if sheet, ok := xlFile.Sheet[sheetName]; ok == true {
111-
for i, row := range sheet.Rows {
109+
for _, row := range sheet.Rows {
112110
//FIXME: I would be nice to optionally only the columns you wanted to output from the sheet...
113111
cells = []string{}
114-
for j, cell := range row.Cells {
115-
val, err := cell.String()
116-
if err != nil {
117-
if permissive == true {
118-
cells = append(cells, fmt.Sprintf("%s", val))
119-
} else {
120-
cells = append(cells, fmt.Sprintf("%s", err))
121-
}
122-
if verbose == true {
123-
fmt.Fprintf(os.Stderr, "row %d, col %d %s\n", i, j, err)
124-
}
125-
} else {
126-
cells = append(cells, val)
127-
}
112+
for _, cell := range row.Cells {
113+
val := cell.String()
114+
cells = append(cells, val)
128115
}
129116
results = append(results, cells)
130117
}
@@ -156,8 +143,6 @@ func init() {
156143
// App Specific Options
157144
flag.BoolVar(&showSheetCount, "c", false, "display number of sheets in Excel Workbook")
158145
flag.BoolVar(&showSheetNames, "n", false, "display sheet names in Excel W9rkbook")
159-
flag.BoolVar(&verbose, "verbose", false, "output cell level errors")
160-
flag.BoolVar(&verbose, "permissive", false, "ignore cell level errors")
161146
}
162147

163148
func main() {

cmds/xlsx2json/xlsx2json.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ func xlsx2JSON(out *os.File, workBookName, sheetName string) error {
109109
for _, row := range sheet.Rows {
110110
cells = []string{}
111111
for _, cell := range row.Cells {
112-
val, err := cell.String()
113-
if err != nil {
114-
//val = fmt.Sprintf("%s", err)
115-
}
112+
val := cell.String()
116113
cells = append(cells, val)
117114
}
118115
results = append(results, cells)

docs/index.html

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ <h1>datatools command help</h1>
2424

2525
<ul>
2626
<li><a href="cmds.html">cmds</a></li>
27-
<li><a href="csvrows.html">csvrows</a></li>
28-
<li><a href="csvjoin.html">csvjoin</a></li>
27+
<li><a href="csv2json.html">csv2json</a></li>
28+
<li><a href="csv2mdtable.html">csv2mdtable</a></li>
2929
<li><a href="csv2xlsx.html">csv2xlsx</a></li>
30+
<li><a href="csvcols.html">csvcols</a></li>
3031
<li><a href="csvfind.html">csvfind</a></li>
31-
<li><a href="jsonmunge.html">jsonmunge</a></li>
32-
<li><a href="csv2json.html">csv2json</a></li>
32+
<li><a href="csvjoin.html">csvjoin</a></li>
33+
<li><a href="csvmerge.html">csvmerge</a></li>
34+
<li><a href="csvrows.html">csvrows</a></li>
3335
<li><a href="jsoncols.html">jsoncols</a></li>
36+
<li><a href="jsonmunge.html">jsonmunge</a></li>
37+
<li><a href="jsonrange.html">jsonrange</a></li>
3438
<li><a href="vcard2json.html">vcard2json</a></li>
3539
<li><a href="xlsx2csv.html">xlsx2csv</a></li>
36-
<li><a href="csv2mdtable.html">csv2mdtable</a></li>
37-
<li><a href="csvcols.html">csvcols</a></li>
38-
<li><a href="jsonrange.html">jsonrange</a></li>
3940
<li><a href="xlsx2json.html">xlsx2json</a></li>
4041
</ul>
4142

docs/index.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
# datatools command help
33

44
+ [cmds](cmds.html)
5-
+ [csvrows](csvrows.html)
6-
+ [csvjoin](csvjoin.html)
5+
+ [csv2json](csv2json.html)
6+
+ [csv2mdtable](csv2mdtable.html)
77
+ [csv2xlsx](csv2xlsx.html)
8+
+ [csvcols](csvcols.html)
89
+ [csvfind](csvfind.html)
9-
+ [jsonmunge](jsonmunge.html)
10-
+ [csv2json](csv2json.html)
10+
+ [csvjoin](csvjoin.html)
11+
+ [csvmerge](csvmerge.html)
12+
+ [csvrows](csvrows.html)
1113
+ [jsoncols](jsoncols.html)
14+
+ [jsonmunge](jsonmunge.html)
15+
+ [jsonrange](jsonrange.html)
1216
+ [vcard2json](vcard2json.html)
1317
+ [xlsx2csv](xlsx2csv.html)
14-
+ [csv2mdtable](csv2mdtable.html)
15-
+ [csvcols](csvcols.html)
16-
+ [jsonrange](jsonrange.html)
1718
+ [xlsx2json](xlsx2json.html)

docs/jsonmunge.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,24 @@ <h2>OPTIONS</h2>
5252

5353
<h2>EXAMPLES</h2>
5454

55-
<p>If data.json contained</p>
55+
<p>If person.json contained</p>
5656

5757
<pre><code class="language-json"> {&quot;name&quot;: &quot;Doe, Jane&quot;, &quot;email&quot;:&quot;[email protected]&quot;, &quot;age&quot;: 42}
5858
</code></pre>
5959

6060
<p>and the template, name.tmpl, contained</p>
6161

62-
<pre><code> {{- .name -}}
62+
<pre><code class="language-template"> {{- .name -}}
6363
</code></pre>
6464

6565
<p>Getting just the name could be done with</p>
6666

67-
<pre><code class="language-shell"> cat data.json | jsonmunge name.tmpl
67+
<pre><code class="language-shell"> cat person.json | jsonmunge name.tmpl
6868
</code></pre>
6969

7070
<p>This would yeild</p>
7171

72-
<pre><code> Doe, Jane
72+
<pre><code class="language-shell"> &quot;Doe, Jane&quot;
7373
</code></pre>
7474

7575
<p>jsonmunge v0.0.9</p>

docs/jsonmunge.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# jsonmunge
3+
4+
## USAGE
5+
6+
jsonmunge [OPTIONS] TEMPLATE_FILENAME
7+
8+
## SYSNOPSIS
9+
10+
jsonmunge is a command line tool that takes a JSON document and
11+
one or more Go templates rendering the results. Useful for
12+
reshaping a JSON document, transforming into a new format,
13+
or filter for specific content.
14+
15+
+ TEMPLATE_FILENAME is the name of a Go text tempate file used to render
16+
the outbound JSON document
17+
18+
## OPTIONS
19+
20+
```
21+
-h display help
22+
-i input filename
23+
-input input filename
24+
-l display license
25+
-o output filename
26+
-output output filename
27+
-v display version
28+
```
29+
30+
## EXAMPLES
31+
32+
If person.json contained
33+
34+
```json
35+
{"name": "Doe, Jane", "email":"[email protected]", "age": 42}
36+
```
37+
and the template, name.tmpl, contained
38+
39+
```template
40+
{{- .name -}}
41+
```
42+
Getting just the name could be done with
43+
44+
```shell
45+
cat person.json | jsonmunge name.tmpl
46+
```
47+
This would yeild
48+
49+
```shell
50+
"Doe, Jane"
51+
```
52+
53+
jsonmunge v0.0.9

0 commit comments

Comments
 (0)