-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathray.go
364 lines (289 loc) · 7.75 KB
/
ray.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package ray
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"github.com/google/uuid"
payloads "github.com/octoper/go-ray/payloads"
"github.com/octoper/go-ray/utils"
"os"
"strconv"
"time"
)
// Callable mocks a function that returns a boolean
type Callable = func() bool
type application struct {
uuid string
host string
port int
enabled bool
sentPayloads []payloads.Payload
client utils.Client
}
type request struct {
Uuid string `json:"uuid"`
Payloads interface{} `json:"payloads"`
Meta map[string]string `json:"meta"`
}
var applicationConfig = application{
host: "127.0.0.1",
port: 23517,
enabled: true,
client: *utils.NewClient(),
}
// NewRay creates a New Ray instance
func NewRay() *application {
app := applicationConfig
return &app
}
// Ray Creates a new insatnce of the application also can receive values to send to Ray
func Ray(values ...interface{}) *application {
r := NewRay()
r.uuid = uuid.New().String()
if values != nil {
r.Send(values...)
}
return r
}
// Send Values
func (r *application) Send(values ...interface{}) *application {
var payloadsMap []payloads.Payload
for _, payload := range values {
switch payload.(type) { // nolint:gosimple
case bool:
payloadsMap = append(payloadsMap, payloads.NewBoolPayload(payload.(bool)))
case nil:
payloadsMap = append(payloadsMap, payloads.NewNullPayload())
case string, int, int32, int64, float32, float64, complex64, complex128, uint, uint8, uint16, uint32, uint64:
payloadsMap = append(payloadsMap, payloads.NewCustomPayload(payload, ""))
default:
payloadsMap = append(payloadsMap, payloads.NewDumpPayload(payload))
}
}
return r.SendRequest(payloadsMap...)
}
// Get the UUID
func (r *application) Uuid() string {
return r.uuid
}
// Get the UUID
func (r *application) Client() *utils.Client {
r.client.SetHost(r.Host())
r.client.SetPort(r.Port())
return &r.client
}
// Get the port application is running
func (r *application) Port() int {
return r.port
}
// Set the port application is running
func (r *application) SetPort(port int) {
applicationConfig.port = port
}
// Get the host application is running
func (r *application) Host() string {
return r.host
}
// Set the host application is running
func (r *application) SetHost(host string) {
applicationConfig.host = host
}
// Get Sent Payloads as Json
func (r *application) SentJsonPayloads() ([]byte, error) {
return json.Marshal(applicationConfig.sentPayloads)
}
// Enable sending payloads to Ray
func (r *application) Enable() {
applicationConfig.enabled = true
}
// Prevents sending payloads to Ray
func (r *application) Disable() {
applicationConfig.enabled = false
}
// Check if Ray is enabled
func (r *application) Enabled() bool {
return applicationConfig.enabled
}
// Check if Ray is disabled
func (r *application) Disabled() bool {
return !applicationConfig.enabled
}
// Create New Screen
func (r *application) NewScreen(name string) *application {
return r.SendRequest(payloads.NewNewScreenPayload(name))
}
// Color
func (r *application) Color(color string) *application {
return r.SendRequest(payloads.NewColorPayload(color))
}
// Send custom payload
func (r *application) SendCustom(content interface{}, label string) *application {
return r.SendRequest(payloads.NewCustomPayload(content, label))
}
// Size
func (r *application) Size(size string) *application {
return r.SendRequest(payloads.NewSizePayload(size))
}
// Hide
func (r *application) Hide() *application {
return r.SendRequest(payloads.NewHidePayload())
}
// Hide App
func (r *application) HideApp() *application {
return r.SendRequest(payloads.NewHideAppPayload())
}
// Show App
func (r *application) ShowApp() *application {
return r.SendRequest(payloads.NewShowAppPayload())
}
// Clear Screen
func (r *application) ClearScreen() *application {
return r.SendRequest(payloads.NewClearScreenPayload())
}
// Clear All
func (r *application) ClearAll() *application {
return r.SendRequest(payloads.NewClearAllPayload())
}
// HTML
func (r *application) Html(html string) *application {
return r.SendRequest(payloads.NewHtmlPayload(html))
}
// Notify
func (r *application) Notify(text string) *application {
return r.SendRequest(payloads.NewNotifyPayload(text))
}
// Boolean
func (r *application) Bool(bool bool) *application {
return r.SendRequest(payloads.NewBoolPayload(bool))
}
// Null
func (r *application) Null() *application {
return r.SendRequest(payloads.NewNullPayload())
}
// Charles
func (r *application) Charles() *application {
return r.Send("🎶 🎹 🎷 🕺")
}
// String
func (r *application) String(str string) *application {
return r.SendRequest(payloads.NewStringPayload(str))
}
//Time
func (r *application) Time(time time.Time) *application {
return r.SendRequest(payloads.NewTimePayload(time, "2006-01-02 15:04:05"))
}
// Time with Format
func (r *application) TimeWithFormat(time time.Time, format string) *application {
return r.SendRequest(payloads.NewTimePayload(time, format))
}
// Pause code execution
func (r *application) Pause() *application {
hash := md5.New()
_, err := hash.Write([]byte(time.Now().String()))
if err != nil {
panic(err)
}
lockName := hash.Sum(nil)
r.SendRequest(payloads.NewCreateLockPayload(hex.EncodeToString(lockName)))
for {
time.Sleep(time.Second);
lockExistsClient := r.Client().LockExists(hex.EncodeToString(lockName))
if !lockExistsClient.Active {
break
}
}
return r
}
// Sends the provided value(s) encoded as a JSON string using json.Marshal.
func (r *application) ToJson(jsons ...interface{}) *application {
for _, jsonValue := range jsons {
r.SendRequest(payloads.NewJsonStringPayload(jsonValue))
}
return r
}
// Image
func (r *application) Image(value string) *application {
return r.SendRequest(payloads.NewImagePayload(value))
}
// Ban
func (r *application) Ban() *application {
return r.Send("🕶")
}
// Die
func (r *application) Die() {
r.DieWithStatusCode(1)
}
// Die with Status Code
func (r *application) DieWithStatusCode(status int) {
os.Exit(status)
}
// Remove
func (r *application) Remove() *application {
return r.SendRequest(payloads.NewRemovePayload())
}
// Show When
func (r *application) ShowWhen(show interface{}) *application {
switch show.(type) { // nolint:gosimple
case Callable:
show = show.(Callable)()
}
if !show.(bool) {
return r.Remove()
}
return r
}
// Show If
func (r *application) ShowIf(show interface{}) *application {
return r.ShowWhen(show)
}
// Remove When
func (r *application) RemoveWhen(show interface{}) *application {
switch show.(type) { // nolint:gosimple
case Callable:
show = show.(Callable)()
}
if show.(bool) {
return r.Remove()
}
return r
}
// Remove If
func (r *application) RemoveIf(show interface{}) *application {
return r.RemoveWhen(show)
}
// Set the host application is running
func (r *application) SendRequest(ResponsePayloads ...payloads.Payload) *application {
var payloadsMap []payloads.Payload
// stack := utils.NewStacktrace()
stackAbsPath := ""
stackLineNo := ""
/*if len(stack.Frames) > 0 {
stackAbsPath = stack.Frames[0].AbsPath
stackLineNo = strconv.Itoa(stack.Frames[0].Lineno)
}*/
for _, payload := range ResponsePayloads {
payload.Origin = map[string]string{
"file": stackAbsPath,
"line_number": stackLineNo,
}
payloadsMap = append(payloadsMap, payload)
}
applicationConfig.sentPayloads = payloadsMap
requestPayload := request{
Uuid: r.Uuid(),
Payloads: payloadsMap,
Meta: map[string]string{
"ray_package_version": "0.1.4",
},
}
if !r.enabled {
return r
}
client := r.Client()
_, err := client.Sent(requestPayload)
//Handle Error
if err != nil {
panic("Couldn't connect to Ray It doesn't seem to be running at " + Ray().Host() + ":" + strconv.Itoa(Ray().Port()))
}
return r
}