-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinteractive.go
91 lines (75 loc) · 1.53 KB
/
interactive.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
package main
import (
"bufio"
"bytes"
"io"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
func boiInteractive() {
userin := bufio.NewReader(os.Stdin)
var lastContext *BoiContext = nil
for {
text, err := userin.ReadBytes('\n')
if err != nil {
boiError(err)
break
}
lex := NewBoiInterpreter(text)
if lastContext != nil {
lex.context = lastContext
}
if err := lex.Run(); err != nil {
boiError(err)
continue
}
lastContext = lex.context
}
}
func boiSlackServer(hostname string) {
var lastContext *BoiContext = nil
r := gin.Default()
r.POST("/", func(c *gin.Context) {
text := c.PostForm("text")
lex := NewBoiInterpreter([]byte(text))
if lastContext != nil {
lex.context = lastContext
}
// === + Terrible Hack Start ===
runBoi := func() (string, error) {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
outc := make(chan string)
go func() {
var buff bytes.Buffer
io.Copy(&buff, r)
outc <- buff.String()
}()
err := lex.Run()
w.Close()
os.Stdout = old
return <-outc, err
}
output, err := runBoi()
if err != nil {
output += "\n\nExited with error: " + err.Error()
c.JSON(http.StatusOK, struct {
ResponseType string `json:"response_type"`
Text string `json:"text"`
}{
"in_channel", output,
})
} else {
c.JSON(http.StatusOK, struct {
ResponseType string `json:"response_type"`
Text string `json:"text"`
}{
"in_channel", output,
})
}
lastContext = lex.context
})
r.Run(hostname)
}