-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (107 loc) · 2.63 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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
)
var mappers = make([]mapper, 0)
type mapper struct {
in io.Writer
out *bufio.Reader
}
var jsonOut = json.NewEncoder(os.Stdout)
func main() {
log("Starting")
lineReader := bufio.NewReader(os.Stdin)
// We read data that looks like ["cmd", <blabla>]
for {
line, err := lineReader.ReadString('\n')
if err != nil {
logerror("error_readinput", fmt.Sprintf("Error reading input:%s", err))
}
parts := strings.SplitN(line, ",", 2)
cmd := strings.Trim(parts[0], "[\" ")
args := strings.Trim(parts[1], "] \n")
switch cmd {
case "reset":
jsonOut.Encode(true)
case "add_fun":
filter, err := strconv.Unquote(args)
if err != nil {
logerror("invalid_function", "This function is invalid:"+err.Error())
continue
}
jqCmd := exec.Command("jq", "-c", "--unbuffered", "-r", filter)
in, err := jqCmd.StdinPipe()
if err != nil {
logerror("error_installing_jq", fmt.Sprintf("Error installing jq: %s", err))
return
}
out, err := jqCmd.StdoutPipe()
if err != nil {
logerror("error_installing_jq", fmt.Sprintf("Error installing jq: %s", err))
return
}
stdErr, err := jqCmd.StderrPipe()
if err != nil {
logerror("error_installing_jq", fmt.Sprintf("Error installing jq: %s", err))
return
}
err = jqCmd.Start()
if err != nil {
logerror("error_installing_jq", fmt.Sprintf("Error installing jq: %s", err))
return
}
mappers = append(mappers, mapper{in, bufio.NewReader(out)})
go func() {
for {
rd := bufio.NewScanner(stdErr)
for rd.Scan() {
err := rd.Text()
logerror("jq_process", fmt.Sprintf("Error in jq subprocess: %s", err))
}
}
}()
log("Installed jq")
jsonOut.Encode(true)
case "map_doc":
var buf bytes.Buffer
buf.WriteString("[")
allmappers:
for i, m := range mappers {
io.WriteString(m.in, args+"\n")
for {
line, isPrefix, err := m.out.ReadLine()
if err != nil {
logerror("error_calling_jq", fmt.Sprintf("Error calling jq: %s", err))
continue allmappers
}
buf.Write(line)
if !isPrefix {
break
}
}
if i != len(mappers)-1 {
buf.WriteString(",")
}
}
io.Copy(os.Stdout, &buf)
io.WriteString(os.Stdout, "]\n")
default:
logerror("unknown_command", fmt.Sprintf("%s is unknown", cmd))
log(fmt.Sprintf("unknown command: %s", cmd))
}
}
}
func logerror(typ, message string) {
fmt.Fprintln(os.Stdout, `["error", "`+typ+`", "`+message+`"]`)
}
func log(message string) {
fmt.Fprintln(os.Stdout, `["log", "`+message+`"]`)
}