-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjpath.go
93 lines (84 loc) · 2.62 KB
/
jpath.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
package main
import (
"fmt"
"github.com/prashanth-hegde/jpath/common"
"github.com/prashanth-hegde/jpath/input"
"github.com/prashanth-hegde/jpath/output"
"github.com/prashanth-hegde/jpath/parser"
"github.com/spf13/cobra"
"os"
"strings"
)
// jpathAppRunner the main usage of jPath application
// If jpath is used as a library in other applications, this is how it is
func jpathAppRunner(parsedJson []byte) {
// tokenize the json doc
tokenized, err := common.Tokenize(parsedJson)
if err != nil {
common.ExitWithMessage("error: " + err.Error())
}
// parse the expression
parsedOutput, err := parser.ProcessExpression(common.Conf.Expr, tokenized)
if err != nil {
common.ExitWithMessage("error: " + err.Error())
}
// print the output
err = output.PrintOutput(parsedOutput)
if err != nil {
common.ExitWithMessage(err.Error())
}
}
func streamOutput(outChannel <-chan []byte) {
for {
doc := <-outChannel
if len(doc) > 0 {
jpathAppRunner(doc)
common.Conf.Wg.Done()
}
}
}
func main() {
// mandatory variables
var json string
// root command parser
var rootCmd = &cobra.Command{
Use: "jpath <expression> <json>",
Short: "analyzer for json data",
Long: `An easy to use json filter to analyze json documents
Complete documentation is available at https://github.com/prashanth-hegde/jpath`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 1:
common.Conf.Expr = strings.TrimSpace(args[0])
json = ""
case 2:
common.Conf.Expr = strings.TrimSpace(args[0])
json = strings.TrimSpace(args[1])
default:
}
},
}
rootCmd.Flags().BoolVarP(&common.Conf.Table, "table", "t", false, "print output as table")
rootCmd.Flags().BoolVarP(&common.Conf.Unwrap, "unwrap", "u", false, "unwrap the output from array")
rootCmd.Flags().BoolVarP(&common.Conf.Compress, "compress", "c", false, "compress the output")
rootCmd.Flags().StringSliceVarP(&common.Conf.Headers, "header", "H", []string{}, "headers to be included for requests")
// parse input args
if err := rootCmd.Execute(); err != nil {
//_, _ = fmt.Fprintf(os.Stderr, "\n%s\n\n%s\n", err.Error(), rootCmd.UsageString())
os.Exit(1)
} else if common.Conf.Expr == "" && json == "" {
_, _ = fmt.Fprintf(os.Stderr, "\n%s\n\n%s\n", "no expression or json document provided", rootCmd.UsageString())
os.Exit(1)
}
// if unwrap option is selected, create an output channel
if common.Conf.Unwrap {
go streamOutput(common.Conf.Channel)
}
// parse the input json
jsonb, err := input.ParseInputJson(json)
if err != nil {
common.ExitWithMessage(err.Error())
}
jpathAppRunner(jsonb)
}