-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
151 lines (125 loc) · 3.24 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
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/itchyny/gojq"
"github.com/mitchellh/go-homedir"
"github.com/therecipe/qt/widgets"
)
var filterValue = "."
var inputValue = `[{
"fruit": "mango"
}, {
"fruit": "banana"
}]`
var loadfileDialog *widgets.QFileDialog
var input *widgets.QPlainTextEdit
var filter *widgets.QLineEdit
var output *widgets.QPlainTextEdit
func main() {
app := widgets.NewQApplication(len(os.Args), os.Args)
if len(os.Args) > 1 {
filterValue = os.Args[1]
}
if len(os.Args) > 2 {
b, err := ioutil.ReadFile(os.Args[2])
if err != nil {
log.Fatal("failed to read " + os.Args[2] + " : " + err.Error())
}
inputValue = string(b)
} else {
if m, _ := os.Stdin.Stat(); m.Mode()&os.ModeCharDevice != os.ModeCharDevice {
b, err := ioutil.ReadAll(os.Stdin)
if err == nil {
inputValue = string(b)
}
}
}
window := widgets.NewQMainWindow(nil, 0)
window.SetMinimumSize2(400, 500)
window.SetWindowTitle("jqview")
dir, _ := homedir.Dir()
loadfileDialog = widgets.NewQFileDialog2(nil, "Select a JSON file", dir, "")
loadfileDialog.ConnectFileSelected(func(filepath string) {
b, err := ioutil.ReadFile(filepath)
if err != nil {
log.Print("failed to read " + filepath + " : " + err.Error())
} else {
input.SetPlainText(string(b))
go refresh()
}
})
loadfileButton := widgets.NewQPushButton2("Load", nil)
loadfileButton.SetMaximumWidth(40)
loadfileButton.ConnectClicked(func(_ bool) {
loadfileDialog.Open(nil, "")
})
input = widgets.NewQPlainTextEdit(nil)
input.SetPlaceholderText("JSON input")
input.SetPlainText(inputValue)
input.ConnectTextChanged(refresh)
inputSection := widgets.NewQWidget(nil, 0)
inputSection.SetLayout(widgets.NewQHBoxLayout())
inputSection.SetMaximumHeight(150)
inputSection.Layout().AddWidget(loadfileButton)
inputSection.Layout().AddWidget(input)
filter = widgets.NewQLineEdit(nil)
filter.SetPlaceholderText("jq filter")
filter.SetText(filterValue)
filter.ConnectTextChanged(func(value string) {
filterValue = value
go refresh()
})
output = widgets.NewQPlainTextEdit(nil)
output.SetSizeAdjustPolicy(widgets.QAbstractScrollArea__AdjustToContents)
output.SetMinimumHeight(300)
widget := widgets.NewQWidget(nil, 0)
widget.SetLayout(widgets.NewQVBoxLayout())
widget.Layout().AddWidget(inputSection)
widget.Layout().AddWidget(filter)
widget.Layout().AddWidget(output)
window.Show()
window.SetCentralWidget(widget)
go refresh()
app.Exec()
}
func refresh() {
inputValue = input.ToPlainText()
output.SetPlainText(runJQ(context.Background(), inputValue, filterValue))
}
func runJQ(
ctx context.Context,
input string,
filter string,
) string {
ctx, cancel := context.WithTimeout(ctx, time.Second*20)
defer cancel()
var object interface{}
err := json.Unmarshal([]byte(input), &object)
if err != nil {
return err.Error()
}
query, err := gojq.Parse(filter)
if err != nil {
return err.Error()
}
iter := query.RunWithContext(ctx, object)
var results []string
for {
v, exists := iter.Next()
if !exists {
break
}
if err, ok := v.(error); ok {
return err.Error()
}
s, _ := json.MarshalIndent(v, "", " ")
results = append(results, string(s))
}
return strings.Join(results, "\n")
}