-
Notifications
You must be signed in to change notification settings - Fork 2
/
go.go
178 lines (128 loc) · 2.72 KB
/
go.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
package main
import (
"encoding/json"
"fmt"
"html"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const GoHelloWorld = `
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, code-playground")
}
`
type GoCompile struct {
Errors string `json:"Errors"`
Events []GoCompileEvent `json:"Events"`
Status int `json:"Status"`
IsTest bool `json:"IsTest"`
TestsFailed int `json:"TestsFailed"`
VetOK bool `json:"VetOK"`
}
type GoCompileEvent struct {
Message string `json:"Message"`
Kind string `json:"Kind"`
Delay int32 `json:"Delay"`
}
type Go struct {
Code string
}
func (r *Go) Type() string {
return ".go"
}
func (g *Go) Init(code string) {
g.Code = code
}
func (g *Go) Default() string {
return GoHelloWorld
}
func (g *Go) Evaluate() {
Spinner.Start()
defer Spinner.Stop()
values := url.Values{
"version": {"2"},
"body": {g.Code},
"withVet": {"true"},
}
values.Add("version", "2")
values.Add("body", g.Code)
values.Add("withVet", "true")
response, err := http.PostForm("https://play.golang.org/compile", values)
if err != nil {
Log.Error(err)
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
Log.Error(err)
return
}
result := &GoCompile{}
err = json.Unmarshal(body, result)
if err != nil {
Log.Error(err)
return
}
result.Execute()
}
func (g *Go) Share() {
Spinner.Start()
defer Spinner.Stop()
response, err := http.Post("https://play.golang.org/share", "text/plain", strings.NewReader(g.Code))
if err != nil {
Log.Error(err)
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
Log.Error(err)
return
}
var result = string(body)
Log.Info("https://play.golang.org/p/" + result)
}
func (g *Go) Import(path string) string {
Spinner.Start()
defer Spinner.Stop()
if !strings.HasPrefix(path, "https://play.golang.org/p/") {
Log.Error("Import path is not valid")
return ""
}
response, err := http.Get(path)
if err != nil {
Log.Error(err)
return ""
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
Log.Error(err)
return ""
}
result := string(body)
tokens := strings.Split(result, "id=\"code\"")
tokens = strings.Split(tokens[1], ">")
tokens = strings.Split(tokens[1], "<")
result = html.UnescapeString(tokens[0])
Spinner.Stop()
return result
}
func (g *GoCompile) Execute() {
Spinner.Stop()
fmt.Println("-------------------------------------------------")
if g.VetOK {
for _, event := range g.Events {
time.Sleep(time.Duration(event.Delay))
fmt.Print(event.Message)
}
} else {
Log.Error(g.Errors)
}
fmt.Println("-------------------------------------------------")
}