-
Notifications
You must be signed in to change notification settings - Fork 4
/
add.go
307 lines (270 loc) · 6.08 KB
/
add.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"fmt"
"strconv"
"strings"
"github.com/Jeffail/gabs/v2"
"github.com/urfave/cli/v2"
)
var cmdAdd cli.Command
type addObjectOptions struct {
json *gabs.Container
path string
delimiter string
keys []string
values []string
}
type addArrayOptions struct {
json *gabs.Container
path string
delimiter string
values []string
}
type addArrayElementOptions struct {
json *gabs.Container
path string
delimiter string
key string
value string
exists bool
}
func init() {
cmdAdd = cli.Command{
Name: "add",
Usage: "add data to a json file",
Subcommands: []*cli.Command{
{
Name: "object",
Usage: "Add a new key/value pair.",
Action: actionAddObject,
Flags: []cli.Flag{
&flagFile,
&flagPath,
&flagDelimiter,
&flagMultiKey,
&flagMultiValue,
&flagPretty,
},
},
{
Name: "array",
Usage: "Add a new array",
Action: actionAddArray,
Flags: []cli.Flag{
&flagFile,
&flagPath,
&flagDelimiter,
&flagMultiValue,
&flagPretty,
},
},
{
Name: "element",
Usage: "Add a new array element",
Action: actionAddArrayElement,
Flags: []cli.Flag{
&flagFile,
&flagPath,
&flagDelimiter,
&flagMultiKey,
&flagMultiValue,
&flagExists,
&flagPretty,
},
},
},
}
}
func actionAddObject(c *cli.Context) error {
j, err := readInput(c.String("file"))
if err != nil {
return err
}
options := addObjectOptions{
json: j,
path: c.String("path"),
delimiter: getDelimiter(c.String("delimiter")),
keys: c.StringSlice("key"),
values: c.StringSlice("value"),
}
j, err = addObject(options)
if err != nil {
return err
}
if pretty {
fmt.Printf(j.StringIndent("", " "))
} else {
fmt.Printf(j.String())
}
return nil
}
func actionAddArray(c *cli.Context) error {
j, err := readInput(c.String("file"))
if err != nil {
return err
}
options := addArrayOptions{
json: j,
path: c.String("path"),
delimiter: getDelimiter(c.String("delimiter")),
values: c.StringSlice("value"),
}
j, err = addArray(options)
if err != nil {
return err
}
if pretty {
fmt.Printf(j.StringIndent("", " "))
} else {
fmt.Printf(j.String())
}
return nil
}
func actionAddArrayElement(c *cli.Context) error {
j, err := readInput(c.String("file"))
if err != nil {
return err
}
options := addArrayElementOptions{
json: j,
path: c.String("path"),
delimiter: getDelimiter(c.String("delimiter")),
key: c.String("key"),
value: c.String("value"),
exists: c.Bool("exists"),
}
j, err = addArrayElement(options)
if err != nil {
return err
}
return nil
}
func addObject(options addObjectOptions) (*gabs.Container, error) {
var err error
var isArray bool
kv := make(map[string]interface{})
j := options.json
// at least one value is required
if len(options.values) < 1 {
return nil, fmt.Errorf("A value is required")
}
splitPath := []string{}
if options.path != "" {
splitPath = strings.Split(options.path, options.delimiter)
}
// Check and see if the destination path is an array
getOpt := getOptions{
json: options.json,
path: options.path,
delimiter: options.delimiter,
}
p, err := get(getOpt)
if err == nil {
if _, ok := p.Data().([]interface{}); ok {
isArray = true
}
}
// if no keys were specified, only allow a single value
for i, v := range options.values {
if isArray {
if len(options.keys) > i {
if x, e := strconv.Atoi(v); e == nil {
kv[options.keys[i]] = x
} else if b, e := strconv.ParseBool(v); e == nil {
kv[options.keys[i]] = b
} else if v == "{}" {
kv[options.keys[i]] = map[string]interface{}{}
} else if v == "[]" {
kv[options.keys[i]] = []interface{}{}
} else {
kv[options.keys[i]] = v
}
}
} else {
sp := []string{}
if len(options.keys) > i {
sp = append(splitPath, options.keys[i])
} else {
if i > 1 {
break
}
sp = splitPath
}
if i, e := strconv.Atoi(v); e == nil {
_, err = j.Set(i, sp...)
} else if b, e := strconv.ParseBool(v); e == nil {
_, err = j.Set(b, sp...)
} else if v == "{}" {
_, err = j.Object(sp...)
} else if v == "[]" {
_, err = j.Array(sp...)
} else {
_, err = j.Set(v, sp...)
}
}
}
if isArray {
err := j.ArrayAppend(kv, splitPath...)
if err != nil {
return nil, err
}
}
return j, err
}
func addArray(options addArrayOptions) (*gabs.Container, error) {
var err error
j := options.json
splitPath := strings.Split(options.path, options.delimiter)
_, err = j.Array(splitPath...)
if err != nil {
return nil, err
}
for _, v := range options.values {
if i, e := strconv.Atoi(v); e == nil {
err = j.ArrayAppend(i, splitPath...)
} else {
err = j.ArrayAppend(v, splitPath...)
}
}
return j, err
}
func addArrayElement(options addArrayElementOptions) (*gabs.Container, error) {
var err error
j := options.json
splitPath := strings.Split(options.path, options.delimiter)
if options.exists {
var searchPath string
if options.key != "" {
searchPath = fmt.Sprintf("%s%s*%s%s=%s", options.path, options.delimiter, options.delimiter, options.key, options.value)
} else {
searchPath = fmt.Sprintf("%s=%s", options.path, options.value)
}
getOptions := getOptions{
json: j,
path: searchPath,
delimiter: options.delimiter,
}
if _, err = get(getOptions); err == nil {
return j, nil
}
}
pathPieces := strings.Split(options.path, options.delimiter)
if index, err := strconv.Atoi(pathPieces[len(pathPieces)-1]); err == nil {
if index >= 0 {
arrPath := strings.Join(pathPieces[:len(pathPieces)-1], options.delimiter)
arr := j.Path(arrPath)
if i, err := strconv.Atoi(options.value); err == nil {
_, err = arr.SetIndex(i, index)
} else {
_, err = arr.SetIndex(options.value, index)
}
}
} else {
if i, err := strconv.Atoi(options.value); err == nil {
err = j.ArrayAppend(i, splitPath...)
} else {
err = j.ArrayAppend(options.value, splitPath...)
}
}
return j, err
}