/
main.go
259 lines (249 loc) · 5.66 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strconv"
isatty "github.com/mattn/go-isatty"
"github.com/tidwall/gjson"
"github.com/tidwall/pretty"
"github.com/tidwall/sjson"
)
var (
version = "0.0.1"
tag = "jj - JSON Stream Editor " + version
usage = `
usage: jj [-v value] [-purnOD] [-i infile] [-o outfile] keypath
examples: jj keypath read value from stdin
or: jj -i infile keypath read value from infile
or: jj -v value keypath edit value
or: jj -v value -o outfile keypath edit value and write to outfile
options:
-v value Edit JSON key path value
-p Make json pretty, keypath is optional
-u Make json ugly, keypath is optional
-r Use raw values, otherwise types are auto-detected
-n Do not output color or extra formatting
-O Performance boost for value updates
-D Delete the value at the specified key path
-l Output array values on multiple lines
-i infile Use input file instead of stdin
-o outfile Use output file instead of stdout
keypath JSON key path (like "name.last")
for more info: https://github.com/tidwall/jj
`
)
type args struct {
infile *string
outfile *string
value *string
raw bool
del bool
opt bool
keypathok bool
keypath string
pretty bool
ugly bool
notty bool
lines bool
}
func parseArgs() args {
fail := func(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "%s\n", tag)
if format != "" {
fmt.Fprintf(os.Stderr, format+"\n", args...)
}
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1)
}
help := func() {
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "%s\n", tag)
fmt.Fprintf(buf, "%s\n", usage)
os.Stdout.Write(buf.Bytes())
os.Exit(0)
}
var a args
for i := 1; i < len(os.Args); i++ {
switch os.Args[i] {
default:
if len(os.Args[i]) > 1 && os.Args[i][0] == '-' {
for j := 1; j < len(os.Args[i]); j++ {
switch os.Args[i][j] {
default:
fail("unknown option argument: \"-%c\"", os.Args[i][j])
case '-':
fail("unknown option argument: \"%s\"", os.Args[i])
case 'p':
a.pretty = true
case 'u':
a.ugly = true
case 'r':
a.raw = true
case 'O':
a.opt = true
case 'D':
a.del = true
case 'n':
a.notty = true
case 'l':
a.lines = true
}
}
continue
}
if !a.keypathok {
a.keypathok = true
a.keypath = os.Args[i]
} else {
fail("unknown option argument: \"%s\"", a.keypath)
}
case "-v", "-i", "-o":
arg := os.Args[i]
i++
if i >= len(os.Args) {
fail("argument missing after: \"%s\"", arg)
}
switch arg {
case "-v":
a.value = &os.Args[i]
case "-i":
a.infile = &os.Args[i]
case "-o":
a.outfile = &os.Args[i]
}
case "--force-notty":
a.notty = true
case "--version":
fmt.Fprintf(os.Stdout, "%s\n", tag)
os.Exit(0)
case "-h", "--help", "-?":
help()
}
}
if !a.keypathok && !a.pretty && !a.ugly {
fail("missing required option: \"keypath\"")
}
return a
}
func main() {
a := parseArgs()
var input []byte
var err error
var outb []byte
var outs string
var outa bool
var outt gjson.Type
var f *os.File
if a.infile == nil {
input, err = ioutil.ReadAll(os.Stdin)
} else {
input, err = ioutil.ReadFile(*a.infile)
}
if err != nil {
goto fail
}
if a.del {
outb, err = sjson.DeleteBytes(input, a.keypath)
if err != nil {
goto fail
}
} else if a.value != nil {
raw := a.raw
val := *a.value
if !raw {
switch val {
default:
if len(val) > 0 {
if (val[0] >= '0' && val[0] <= '9') || val[0] == '-' {
if _, err := strconv.ParseFloat(val, 64); err == nil {
raw = true
}
}
}
case "true", "false", "null":
raw = true
}
}
opts := &sjson.Options{}
if a.opt {
opts.Optimistic = true
opts.ReplaceInPlace = true
}
if raw {
// set as raw block
outb, err = sjson.SetRawBytesOptions(
input, a.keypath, []byte(val), opts)
} else {
// set as a string
outb, err = sjson.SetBytesOptions(input, a.keypath, val, opts)
}
if err != nil {
goto fail
}
} else {
if !a.keypathok {
outb = input
} else {
res := gjson.GetBytes(input, a.keypath)
if a.raw {
outs = res.Raw
} else {
outt = res.Type
outa = res.IsArray()
outs = res.String()
}
}
}
if a.outfile == nil {
f = os.Stdout
} else {
f, err = os.Create(*a.outfile)
if err != nil {
goto fail
}
}
if outb == nil {
outb = []byte(outs)
}
if a.lines && outa {
var outb2 []byte
gjson.ParseBytes(outb).ForEach(func(_, v gjson.Result) bool {
outb2 = append(outb2, pretty.Ugly([]byte(v.Raw))...)
outb2 = append(outb2, '\n')
return true
})
outb = outb2
} else if a.raw || outt != gjson.String {
if a.pretty {
outb = pretty.Pretty(outb)
} else if a.ugly {
outb = pretty.Ugly(outb)
}
}
if a.raw && (!a.pretty && !a.ugly) {
outb = pretty.Pretty(outb)
}
if !a.notty && isatty.IsTerminal(f.Fd()) {
if a.raw || outt != gjson.String {
outb = pretty.Color(outb, pretty.TerminalStyle)
} else {
outb = append([]byte(pretty.TerminalStyle.String[0]), outb...)
outb = append(outb, pretty.TerminalStyle.String[1]...)
}
for len(outb) > 0 && outb[len(outb)-1] == '\n' {
outb = outb[:len(outb)-1]
}
outb = append(outb, '\n')
}
if len(outb) > 0 && outb[len(outb)-1] != '\n' {
outb = append(outb, '\n')
}
f.Write(outb)
f.Close()
return
fail:
fmt.Fprintf(os.Stderr, "error: %v\n", err.Error())
os.Exit(1)
}