-
Notifications
You must be signed in to change notification settings - Fork 0
/
guru.go
69 lines (59 loc) · 1.65 KB
/
guru.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
package acn
import (
"bytes"
"encoding/json"
"os"
"os/exec"
"strconv"
)
type guruErr string
func (ge guruErr) Error() string {
return string(ge)
}
const (
ErrGuruAmbiguous guruErr = "ambiguous selection within source file"
)
type GuruDescribeValueTypePos struct {
ObjPos string `json:"objpos"`
Desc string `json:"desc"`
}
type GuruDescribeValue struct {
Type string `json:"type"`
ObjPos string `json:"objpos"`
Typespos []GuruDescribeValueTypePos `json:"typespos"`
}
type GuruDescribeTypeMethod struct {
Name string `json:"name"`
Pos string `json:"pos"`
}
type GuruDescribeType struct {
Type string `json:"type"`
NamePos string `json:"namepos"`
NameDef string `json:"namedef"`
Methods []GuruDescribeTypeMethod `json:"methods"`
}
type GuruDescribeResponse struct {
Desc string `json:"identifier"`
Pos string `json:"pos"`
Detail string `json:"detail"`
Value *GuruDescribeValue `json:"value"`
Type *GuruDescribeType `json:"type"`
}
func RunGuruDescribeQuery(filepath string, position int, resp *GuruDescribeResponse) error {
cmd := exec.Command("guru", "-json", "describe", filepath+":#"+strconv.Itoa(position))
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return err
}
processOut := out.Bytes()
if bytes.Contains(processOut, []byte("ambiguous selection within source file")) {
return ErrGuruAmbiguous
} else if bytes.Contains(processOut, []byte("no such file or directory")) {
return os.ErrNotExist
} else {
err := json.Unmarshal(processOut, resp)
return err
}
}