-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog.go
182 lines (156 loc) · 3.48 KB
/
prog.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
package jqx
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"io/fs"
"iter"
"maps"
"os"
"runtime/debug"
"slices"
"strings"
)
func decoder(r io.Reader, name string, raw bool) iter.Seq[any] {
if raw {
return func(yield func(any) bool) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
if !yield(scanner.Text()) {
break
}
}
err := scanner.Err()
failif(err, "scanning %s", name)
}
}
return func(yield func(any) bool) {
decoder := json.NewDecoder(r)
for {
var v any
err := decoder.Decode(&v)
if err == io.EOF {
return
}
failif(err, "decoding %s", name)
if !yield(v) {
break
}
}
}
}
type Program struct {
Args []string
Open func(string) (fs.File, error)
Find func(string) fs.FS
OutFS fs.FS
Stdin io.Reader
Stdout io.Writer
StdinIsTerminal bool
StdoutIsTerminal bool
}
type flags struct {
script string
filenames []string
dry bool
tab bool
rawIn bool
jsonOut bool
env bool
find string
}
func (f *flags) populate(args []string) {
fset := flag.NewFlagSet("", flag.ExitOnError)
fset.BoolVar(&f.dry, "dry-run", false, `don't persist snapshots`)
fset.BoolVar(&f.tab, "t", false, `(tab) always indent output`)
fset.BoolVar(&f.rawIn, "r", false, `(raw) inputs are newline-separated strings`)
fset.BoolVar(&f.jsonOut, "j", false, `(json) always output json (strings are unwrapped by default)`)
fset.BoolVar(&f.env, "e", false, `(env) enable $env`)
fset.StringVar(&f.find, "find", "", `enable $find`)
usage := fset.Usage
fset.Usage = func() {
usage()
out := fset.Output()
info, _ := debug.ReadBuildInfo()
if info != nil {
fmt.Fprintln(out)
fmt.Fprintln(out, "jqx", info.Main.Version, "built with", info.GoVersion)
fmt.Fprintf(out, "\n\tgo install %s@latest\n\n", info.Path)
}
}
fset.Parse(args)
args = fset.Args()
f.script = "."
if len(args) > 0 {
f.script, f.filenames = args[0], args[1:]
}
}
func (p *Program) Main() (rtErr error) {
defer catch[failError](&rtErr)
var f flags
f.populate(p.Args)
files := map[string]any{}
slices.Values(f.filenames)(func(filename string) bool {
file, err := p.Open(filename)
failif(err, "loading")
defer file.Close()
v := slices.Collect(decoder(file, filename, f.rawIn))
if len(v) == 1 && !f.rawIn {
files[filename] = v[0]
} else {
files[filename] = v
}
return true
})
input := decoder(p.Stdin, "stdin", f.rawIn)
if p.StdinIsTerminal {
input = func(yield func(any) bool) { yield(files) }
}
marshal := getMarshaler(
f.tab || (p.StdoutIsTerminal && !f.jsonOut),
!f.jsonOut,
)
state := State{
Globals: map[string]any{"files": files},
}
if f.env {
envVars := map[string]any{}
for _, v := range os.Environ() {
k, v, _ := strings.Cut(v, "=")
envVars[k] = v
}
state.Globals["env"] = envVars
}
if f.find != "" {
find := map[string]any{}
err := fs.WalkDir(p.Find(f.find), ".", func(path string, d fs.DirEntry, err error) error {
if path == "." {
return err
}
if d.IsDir() {
path = path + "/"
}
find[path] = true
return err
})
failif(err, "finding subdirs")
state.Globals["find"] = find
}
query := state.Compile(constString(f.script))
for v := range input {
for v := range query(v) {
v := must(marshal(v))
fmt.Fprintln(p.Stdout, string(v))
}
}
if f.dry {
for _, file := range slices.Sorted(maps.Keys(state.Files)) {
fmt.Fprintln(p.Stdout, file)
}
state.Files = nil
}
p.OutFS = toFS(state.Files, getMarshaler(f.tab, !f.jsonOut))
return nil
}