-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3jq.go
108 lines (95 loc) · 2.1 KB
/
i3jq.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
package main
import (
"os"
"fmt"
"log"
"flag"
"errors"
"github.com/itchyny/gojq"
)
func main() {
var fileFlag = flag.String("m", "show", "Load module from file")
var quietFlag = flag.Bool("q", false, "Do not print values")
var rawFlag = flag.Bool("r", true, "Print raw values")
var helpFlag = flag.Bool("h", false, "Show help")
flag.Parse()
var args = flag.Args()
if *helpFlag {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION...] [QUERY] [ARGS...]\n\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
loader := i3jqModuleLoader{}
var query_file *gojq.Query
q, err := loader.LoadModule(*fileFlag)
if err != nil {
log.Fatalln(err)
}
query_file = q
var query_arg *gojq.Query
if len(args) > 0 {
q, err := gojq.Parse(args[0])
if err != nil {
log.Fatalln(err)
}
query_arg = q
}
var query *gojq.Query
if query_arg != nil {
loader.base = query_file
query = query_arg
} else {
query = query_file
}
if query != nil {
code, err := gojq.Compile(query,
gojq.WithModuleLoader(&loader),
gojq.WithIterFunction("_i3jq", 3, 3, func(_ any, xs []any) gojq.Iter {
messageType, ok0 := xs[0].(int)
if !ok0 {
return gojq.NewIter(errors.New("messageType param must be an int"))
}
keepAlive, ok2 := xs[2].(bool)
if !ok2 {
return gojq.NewIter(errors.New("keepAlive param must be a bool"))
}
var payload *string = nil
if str, ok := xs[1].(string); ok {
payload = &str
} else if xs[1] != nil {
return gojq.NewIter(errors.New("payload param must be a string"))
}
iter, err := i3jq_ipc(messageType, payload, keepAlive)
if err != nil {
return gojq.NewIter(err)
} else {
return iter
}
}),
)
if err != nil {
log.Fatalln(err)
}
iter := code.Run(nil)
for {
val, ok := iter.Next()
if !ok {
break
}
if err, ok := val.(error); ok {
log.Fatalln(err)
}
if !*quietFlag {
if str, ok := val.(string); (*rawFlag) && ok {
fmt.Println(str)
} else {
output, err := gojq.Marshal(val)
if err != nil {
log.Fatalln(err)
}
fmt.Println(string(output))
}
}
}
}
}