-
Notifications
You must be signed in to change notification settings - Fork 4
/
del.go
69 lines (59 loc) · 1.12 KB
/
del.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
package main
import (
"fmt"
"strings"
"github.com/Jeffail/gabs/v2"
"github.com/urfave/cli/v2"
)
var cmdDel cli.Command
type delKeyOptions struct {
json *gabs.Container
path string
delimiter string
}
func init() {
cmdDel = cli.Command{
Name: "del",
Usage: "delete data from a json file",
Subcommands: []*cli.Command{
{
Name: "key",
Usage: "Delete a new key/value pair.",
Action: actionDelKey,
Flags: []cli.Flag{
&flagFile,
&flagPath,
&flagDelimiter,
&flagPretty,
},
},
},
}
}
func actionDelKey(c *cli.Context) error {
j, err := readInput(c.String("file"))
if err != nil {
return err
}
options := delKeyOptions{
json: j,
path: c.String("path"),
delimiter: getDelimiter(c.String("delimiter")),
}
j, err = delKey(options)
if err != nil {
return err
}
if pretty {
fmt.Printf(j.StringIndent("", " "))
} else {
fmt.Printf(j.String())
}
return nil
}
func delKey(options delKeyOptions) (*gabs.Container, error) {
j := options.json
path := strings.Split(options.path, options.delimiter)
err := j.Delete(path...)
return j, err
}