-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsq.go
187 lines (159 loc) · 3.03 KB
/
fsq.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
183
184
185
186
187
package main
import (
"encoding/json"
"fmt"
"go/constant"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/nvi-inc/fsgo"
"github.com/peterh/liner"
)
var history_path = filepath.Join(os.TempDir(), "fsqhistory")
func cstr(in interface{}) string {
v := reflect.ValueOf(in)
for v.Kind() == reflect.Ptr {
v = reflect.Indirect(v)
}
var s string
switch v.Kind() {
case reflect.Array, reflect.Slice:
if v.Type().Elem().Kind() != reflect.Uint8 {
panic("argument to \"str\" is not a string")
}
s = string(v.Slice(0, v.Len()).Bytes())
case reflect.String:
s = v.String()
default:
panic("argument to \"str\" is not a string")
}
i := 0
for i < len(s) && s[i] != 0 {
i++
}
return s[:i]
}
func complete(terp *interpreter, line string) (c []string) {
i := len(line)
if i > 0 {
i--
}
// trace backwards to find the beginning of the current sub-expression
for i > 0 && line[i] != '.' && line[i] != '(' && line[i] != '[' {
i--
}
expression := ""
prefix := line[:i]
search := line[i:]
if i > 0 {
// Trim off '.' '(' or '['
prefix = line[:i+1]
search = search[1:]
j := i - 1
nper := 0
nsqb := 0
for j > 0 && line[j] != '=' {
switch line[j] {
case '(':
nper--
case ')':
nper++
case '[':
nsqb--
case ']':
nsqb++
}
if nper < 0 || nsqb < 0 {
break
}
j--
}
if i != j {
expression = line[j:i]
if j != 0 {
// Trim off '=' '(' or '['
expression = expression[1:]
}
}
}
value, err := terp.Eval(fmt.Sprintf("ls(%s)", expression))
if err != nil {
return
}
names, ok := value.Interface().([]string)
if !ok {
return
}
for _, n := range names {
if strings.HasPrefix(n, search) {
c = append(c, prefix+n)
}
}
return
}
func help() {
fmt.Println(`Help should go here
blag
`)
}
func main() {
fsshm, err := fs.Attach()
if err != nil {
fmt.Println("error conencting to the FS:", err)
os.Exit(1)
}
terp := NewInterpreter()
terp.Tag = "json"
terp.Global("fs", fsshm)
terp.Global("str", cstr)
terp.Global("help", help)
lr := liner.NewLiner()
defer lr.Close()
lr.SetCompleter(func(line string) []string {
return complete(terp, line)
})
encoder := json.NewEncoder(os.Stdout)
if liner.TerminalSupported() {
encoder.SetIndent("", " ")
}
for {
line, err := lr.Prompt("> ")
if err != nil { // io.EOF
break
}
lr.AppendHistory(line)
for _, line = range strings.Split(line, ";") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
value, err := terp.Eval(line)
if err != nil {
fmt.Println("error:", err)
continue
}
if !value.IsValid() {
continue
}
switch value.Kind() {
case reflect.Func:
if value.Type().NumIn() == 0 && value.Type().NumOut() == 0 {
value.Call([]reflect.Value{})
continue
}
fmt.Println(value.Type())
continue
case reflect.Struct:
if cv, ok := value.Interface().(constant.Value); ok {
fmt.Println(cv)
continue
}
}
err = encoder.Encode(value.Interface())
if err != nil {
fmt.Println(err)
}
}
}
}