1
- //
2
1
// jsonjoin is a command line tool that takes two JSON documents and combined
3
2
// them into one depending on the options
4
3
//
5
4
// @author R. S. Doiel, <[email protected] >
6
- //
7
5
package main
8
6
9
7
import (
10
- "encoding/json"
11
8
"flag"
12
9
"fmt"
13
10
"io/ioutil"
@@ -20,23 +17,28 @@ import (
20
17
)
21
18
22
19
var (
23
- helpText = `---
24
- title: "json2toml (1) user manual"
25
- author: "R. S. Doiel"
26
- pubDate: 2013-01-06
27
- ---
20
+ helpText = `%{app_name}(1) irdmtools user manual | version {version} {release_hash}
21
+ % R. S. Doiel
22
+ % {release_date}
28
23
29
24
# NAME
30
25
31
- json2toml
26
+ {app_name}
32
27
33
28
# SYNOPSIS
34
29
35
- json2toml [OPTIONS] [ JSON_FILENAME] [TOML_FILENAME ]
30
+ {app_name} [OPTIONS] JSON_FILENAME [JSON_FILENAME ... ]
36
31
37
32
# DESCRIPTION
38
33
39
- json2toml is a tool that converts JSON objects into TOML output.
34
+ {app_name} joins one or more JSON objects. By default the
35
+ objects are each assigned to an attribute corresponding with their
36
+ filenames minus the ".json" extension. If the object is read from
37
+ standard input then "_" is used as it's attribute name.
38
+
39
+ If you use the update or overwrite options you will create a merged
40
+ object. The update option keeps the attribute value first encountered
41
+ and overwrite takes the last attribute value encountered.
40
42
41
43
# OPTIONS
42
44
@@ -61,20 +63,44 @@ display version
61
63
-quiet
62
64
: suppress error messages
63
65
66
+ -create
67
+ : Create a root object placing each joined objects under their own attribute
68
+
69
+ -update
70
+ : update first object with the second object, ignore existing attributes
71
+
72
+ -overwrite
73
+ : update first object with the second object, overwriting existing attributes
64
74
65
75
# EXAMPLES
66
76
67
- These would get the file named "my.json" and save it as my.toml
77
+ This is an example of take "my1.json" and "my2.json"
78
+ render "my.json"
79
+
80
+ ~~~
81
+ jsonjoin my1.json my2.json >my.json
82
+ ~~~
83
+
84
+ my.json would have two attributes, "my1" and "my2" each
85
+ with their complete attributes.
68
86
87
+ Using the update option you can merge my1.json with any additional attribute
88
+ values found in m2.json.
89
+
90
+ ~~~
91
+ jsonjoin -update my1.json my2.json >my.json
69
92
~~~
70
- json2toml my.json > my.toml
71
93
72
- json2toml my.json my.toml
94
+ Using the overwrite option you can merge my1.json with my2.json accepted
95
+ as replacement values.
73
96
74
- cat my.json | json2toml -i - > my.toml
97
+ ~~~
98
+ jsonjoin -overwrite my1.json my2.json >my.json
75
99
~~~
76
100
77
- json2toml 1.2.1
101
+
102
+
103
+
78
104
79
105
80
106
`
@@ -84,7 +110,7 @@ json2toml 1.2.1
84
110
showLicense bool
85
111
showVersion bool
86
112
showExamples bool
87
- inputFName string
113
+ pretty bool
88
114
outputFName string
89
115
generateMarkdown bool
90
116
generateManPage bool
@@ -98,10 +124,6 @@ json2toml 1.2.1
98
124
createRoot bool
99
125
)
100
126
101
- func fmtTxt (src string , appName string , version string ) string {
102
- return strings .ReplaceAll (strings .ReplaceAll (src , "{app_name}" , appName ), "{version}" , version )
103
- }
104
-
105
127
func main () {
106
128
appName := path .Base (os .Args [0 ])
107
129
@@ -110,16 +132,16 @@ func main() {
110
132
flag .BoolVar (& showLicense , "license" , false , "display license" )
111
133
flag .BoolVar (& showVersion , "version" , false , "display version" )
112
134
113
- flag .StringVar (& inputFName , "i" , "" , "input filename (for root object)" )
114
- flag .StringVar (& inputFName , "input" , "" , "input filename (for root object)" )
115
135
flag .StringVar (& outputFName , "o" , "" , "output filename" )
116
136
flag .StringVar (& outputFName , "output" , "" , "output filename" )
117
137
flag .BoolVar (& quiet , "quiet" , false , "suppress error messages" )
118
138
flag .BoolVar (& newLine , "nl" , false , "if true add a trailing newline" )
119
139
flag .BoolVar (& newLine , "newline" , false , "if true add a trailing newline" )
140
+ flag .BoolVar (& pretty , "p" , false , "pretty print json" )
141
+ flag .BoolVar (& pretty , "pretty" , false , "pretty print json" )
120
142
121
143
// Application Specific Options
122
- flag .BoolVar (& createRoot , "create" , false , "create an empty root object, {} " )
144
+ flag .BoolVar (& createRoot , "create" , false , "for each object joined each under their own attribute. " )
123
145
flag .BoolVar (& update , "update" , false , "copy new key/values pairs into root object" )
124
146
flag .BoolVar (& overwrite , "overwrite" , false , "copy all key/values into root object" )
125
147
@@ -134,35 +156,17 @@ func main() {
134
156
out := os .Stdout
135
157
eout := os .Stderr
136
158
137
- if inputFName != "" && inputFName != "-" {
138
- in , err = os .Open (inputFName )
139
- if err != nil {
140
- fmt .Fprintln (eout ,err )
141
- os .Exit (1 )
142
- }
143
- defer in .Close ()
144
- }
145
-
146
- if outputFName != "" && outputFName != "-" {
147
- out , err = os .Create (outputFName )
148
- if err != nil {
149
- fmt .Fprintln (eout , err )
150
- os .Exit (1 )
151
- }
152
- defer out .Close ()
153
- }
154
-
155
159
// Process options
156
160
if showHelp {
157
- fmt .Fprintf (out , "%s\n " , fmtTxt (helpText , appName , datatools .Version ))
161
+ fmt .Fprintf (out , "%s\n " , datatools . FmtHelp (helpText , appName , datatools .Version , datatools . ReleaseDate , datatools . ReleaseHash ))
158
162
os .Exit (0 )
159
163
}
160
164
if showLicense {
161
165
fmt .Fprintf (out , "%s\n " , datatools .LicenseText )
162
166
os .Exit (0 )
163
167
}
164
168
if showVersion {
165
- fmt .Fprintf (out , "%s %s\n " , appName , datatools .Version )
169
+ fmt .Fprintf (out , "%s %s %s \n " , appName , datatools .Version , datatools . ReleaseHash )
166
170
os .Exit (0 )
167
171
}
168
172
if newLine {
@@ -180,27 +184,18 @@ func main() {
180
184
outObject := map [string ]interface {}{}
181
185
newObject := map [string ]interface {}{}
182
186
183
- // READ in the JSON document if present on standard in or specified with -i.
184
- if createRoot == false {
185
- buf , err := ioutil .ReadAll (in )
186
- if err != nil {
187
- fmt .Fprintln (eout , err )
188
- os .Exit (1 )
189
- }
190
- err = json .Unmarshal (buf , & outObject )
191
- if err != nil {
192
- fmt .Fprintln (eout , err )
193
- os .Exit (1 )
194
- }
195
- }
196
-
197
187
for _ , arg := range args {
198
- src , err := ioutil .ReadFile (arg )
199
- if err != nil {
200
- fmt .Fprintln (eout , err )
201
- os .Exit (1 )
188
+ var src []byte
189
+ if arg != "" && arg != "-" {
190
+ src , err = ioutil .ReadFile (arg )
191
+ if err != nil {
192
+ fmt .Fprintln (eout , err )
193
+ os .Exit (1 )
194
+ }
195
+ } else {
196
+ src , err = ioutil .ReadAll (in )
202
197
}
203
- err = json . Unmarshal (src , & newObject )
198
+ err = datatools . JSONUnmarshal (src , & newObject )
204
199
if err != nil {
205
200
fmt .Fprintln (eout , err )
206
201
os .Exit (1 )
@@ -218,11 +213,22 @@ func main() {
218
213
}
219
214
default :
220
215
key := strings .TrimSuffix (path .Base (arg ), ".json" )
216
+ if key == "" {
217
+ key = "_"
218
+ }
221
219
outObject [key ] = newObject
222
220
}
223
221
}
224
222
225
- src , err := json .Marshal (outObject )
223
+ var (
224
+ src []byte
225
+ )
226
+
227
+ if pretty {
228
+ src , err = datatools .JSONMarshalIndent (outObject , "" , " " )
229
+ } else {
230
+ src , err = datatools .JSONMarshal (outObject )
231
+ }
226
232
if err != nil {
227
233
fmt .Fprintln (eout , err )
228
234
os .Exit (1 )
0 commit comments