-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
461 lines (380 loc) · 12.2 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package main
import (
"restman/app"
"restman/components/collections"
"restman/components/config"
"restman/components/importer"
"restman/components/popup"
"restman/components/request"
"restman/components/url"
"restman/utils"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
boxer "github.com/treilik/bubbleboxer"
)
var windows = []string{"collections", "url", "request", "results"}
var (
testStyle = lipgloss.NewStyle().
Bold(true).
Border(lipgloss.NormalBorder()).
BorderForeground(config.COLOR_SUBTLE).
PaddingLeft(1)
testStyleFocused = lipgloss.NewStyle().
Bold(true).
Border(lipgloss.NormalBorder()).
BorderForeground(config.COLOR_HIGHLIGHT).
PaddingLeft(1)
listHeader = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
BorderForeground(config.COLOR_SUBTLE).
Render
)
func stripErr(n boxer.Node, _ error) boxer.Node {
return n
}
func main() {
rootCmd.Flags().StringP("url", "u", "", "Url")
rootCmd.Flags().StringP("data", "d", "", "Data")
rootCmd.Flags().StringP("data-raw", "", "", "Data Raw")
rootCmd.Flags().StringP("request", "X", "GET", "HTTP method")
rootCmd.Flags().StringArrayP("header", "H", []string{}, "HTTP header")
//NOTE: not supported
rootCmd.Flags().StringP("output", "o", "", "Write output to <file> instead of stdout")
rootCmd.Flags().BoolP("verbose", "v", false, "Make the operation more talkative")
rootCmd.Flags().BoolP("silent", "s", false, "Silent mode")
rootCmd.Flags().BoolP("location", "L", false, "Follow redirects")
rootCmd.Flags().StringP("user", "U", "", "Server user and password")
rootCmd.Flags().StringP("proxy", "x", "", "Use proxy on given port")
rootCmd.Flags().BoolP("insecure", "k", false, "Allow insecure server connections when using SSL")
rootCmd.Flags().StringP("cookie", "b", "", "Send cookies from string/file")
rootCmd.Flags().StringP("cookie-jar", "c", "", "Write cookies to <file> after operation")
rootCmd.Flags().StringP("cert", "", "", "Client certificate file and password")
rootCmd.Flags().StringP("key", "", "", "Private key file name")
rootCmd.Flags().StringP("cacert", "", "", "CA certificate to verify peer against")
rootCmd.Flags().StringP("capath", "", "", "CA directory to verify peer against")
rootCmd.Flags().StringP("connect-timeout", "", "", "Maximum time allowed for connection")
rootCmd.Flags().StringP("max-time", "", "", "Maximum time allowed for the transfer")
rootCmd.Flags().BoolP("compressed", "", false, "Request compressed response")
rootCmd.Flags().StringP("user-agent", "", "", "Send User-Agent <name> to server")
rootCmd.Flags().StringP("referer", "", "", "Send Referer <URL> to server")
rootCmd.Execute()
}
type Model struct {
tui boxer.Boxer
focused string
popup tea.Model
collections collections.Collections
initialCall *app.Call
width int
height int
}
func (m Model) Init() tea.Cmd {
var (
focusCmd tea.Cmd
initalCallCmd tea.Cmd
runCmd tea.Cmd
)
m.focused = "url"
m.tui.ModelMap[m.focused], focusCmd = m.tui.ModelMap[m.focused].Update(config.WindowFocusedMsg{State: true})
// set initial call if provided
if m.initialCall != nil && m.initialCall.IsValid() {
initalCallCmd = func() tea.Msg {
return app.CallSelectedMsg{Call: m.initialCall}
}
runCmd = app.GetInstance().GetResponse(m.initialCall)
}
return tea.Sequence(
app.GetInstance().ReadCollectionsFromJSON(),
focusCmd,
initalCallCmd,
runCmd,
)
}
func (m *Model) Next() (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
if m.focused != "" {
var previous tea.Model = m.tui.ModelMap[m.focused]
m.tui.ModelMap[m.focused], cmd = previous.Update(config.WindowFocusedMsg{State: false})
cmds = append(cmds, cmd)
}
coll := m.tui.ModelMap["collections"].(collections.Collections)
switch m.focused {
case "collections":
m.focused = "url"
case "tabs":
m.focused = "url"
case "url":
m.focused = "request"
case "request":
m.focused = "results"
default:
if coll.IsMinified() {
m.focused = "url"
} else {
m.focused = "collections"
}
}
m.tui.ModelMap[m.focused], cmd = m.tui.ModelMap[m.focused].Update(config.WindowFocusedMsg{State: true})
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m *Model) Prev() (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
if m.focused != "" {
var previous tea.Model = m.tui.ModelMap[m.focused]
m.tui.ModelMap[m.focused], cmd = previous.Update(config.WindowFocusedMsg{State: false})
cmds = append(cmds, cmd)
}
coll := m.tui.ModelMap["collections"].(collections.Collections)
switch m.focused {
case "collections":
m.focused = "results"
case "results":
m.focused = "request"
case "request":
m.focused = "url"
default:
if coll.IsMinified() {
m.focused = "results"
} else {
m.focused = "collections"
}
}
m.tui.ModelMap[m.focused], cmd = m.tui.ModelMap[m.focused].Update(config.WindowFocusedMsg{State: true})
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m *Model) SetFocused(newFocused string) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
if m.focused != "" {
var previous tea.Model = m.tui.ModelMap[m.focused]
m.tui.ModelMap[m.focused], cmd = previous.Update(config.WindowFocusedMsg{State: false})
cmds = append(cmds, cmd)
}
m.focused = newFocused
m.tui.ModelMap[m.focused], cmd = m.tui.ModelMap[m.focused].Update(config.WindowFocusedMsg{State: true})
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m Model) getCollectionsPane() *collections.Collections {
collections := m.tui.ModelMap["collections"].(collections.Collections)
return &collections
}
func (m Model) getUrlPane() *url.Url {
url := m.tui.ModelMap["url"].(url.Url)
return &url
}
func (m Model) getRequestPane() *request.Request {
middle := m.tui.ModelMap["request"].(request.Request)
return &middle
}
func (m Model) AddToCollection() tea.Cmd {
url := m.getUrlPane()
coll := m.popup.(collections.AddToCollection)
call := url.Call()
call.Url = url.Value()
call.Method = url.Method()
return app.GetInstance().AddToCollection(coll.CollectionName(), call)
}
func (m Model) GetFadedView() string {
return lipgloss.NewStyle().Foreground(config.COLOR_SUBTLE).Render(utils.RemoveANSI(m.View()))
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
// ----------------------------
// POPUP LOGIC
switch msg := msg.(type) {
case popup.ChoiceResultMsg:
m.popup = nil
if msg.Result {
return m, tea.Quit
}
case popup.ClosePopupMsg:
m.popup = nil
case app.CallSelectedMsg:
m.SetFocused("url")
case tea.MouseMsg:
if msg.Type == tea.MouseLeft {
if zone.Get("method").InBounds(msg) {
m.SetFocused("url")
url := m.getUrlPane()
url.CycleOverMethods()
m.tui.ModelMap["url"] = url
} else if zone.Get("input").InBounds(msg) {
m.SetFocused("url")
} else if zone.Get("send").InBounds(msg) {
m.SetFocused("url")
url := m.getUrlPane()
_, cmd = url.Submit()
m.tui.ModelMap["url"] = url
return m, cmd
} else if zone.Get("save").InBounds(msg) {
m.SetFocused("url")
url := m.getUrlPane()
coll := collections.NewAddToCollection(m.GetFadedView(), 40, m.tui.LayoutTree.GetWidth())
coll.SetUrl(url.Value())
m.popup = coll
return m, m.popup.Init()
} else if zone.Get("add_to_collection_cancel").InBounds(msg) {
m.popup = nil
return m, nil
} else if zone.Get("add_to_collection_save").InBounds(msg) {
cmd := m.AddToCollection()
m.popup = nil
return m, cmd
} else if zone.Get("collections_minified").InBounds(msg) {
m.tui.ModelMap["collections"], cmd = m.tui.ModelMap["collections"].(collections.Collections).SetMinified(false)
m.tui.UpdateSize(tea.WindowSizeMsg{Width: m.tui.LayoutTree.GetWidth(), Height: m.tui.LayoutTree.GetHeight()})
return m.SetFocused("collections")
} else if zone.Get("tab_Results").InBounds(msg) {
m.SetFocused("results")
} else if zone.Get("tab_Params").InBounds(msg) {
m.SetFocused("request")
request := m.getRequestPane()
request.SetActiveTab(0)
m.tui.ModelMap["request"] = request
} else if zone.Get("tab_Headers").InBounds(msg) {
m.SetFocused("request")
request := m.getRequestPane()
request.SetActiveTab(1)
m.tui.ModelMap["request"] = request
} else if zone.Get("tab_Auth").InBounds(msg) {
m.SetFocused("request")
request := m.getRequestPane()
request.SetActiveTab(2)
m.tui.ModelMap["request"] = request
} else if zone.Get("tab_Body").InBounds(msg) {
m.SetFocused("request")
request := m.getRequestPane()
request.SetActiveTab(3)
m.tui.ModelMap["request"] = request
} else if zone.Get("collections_minify").InBounds(msg) {
m.tui.ModelMap["collections"], cmd = m.tui.ModelMap["collections"].(collections.Collections).SetMinified(true)
m.tui.UpdateSize(tea.WindowSizeMsg{Width: m.tui.LayoutTree.GetWidth(), Height: m.tui.LayoutTree.GetHeight()})
return m.SetFocused("url")
} else if zone.Get("collections").InBounds(msg) {
return m.SetFocused("collections")
}
}
// TODO: refactor to use in the same way as AddToCollection
case collections.CreateResultMsg:
m.popup = nil
return m, cmd
case importer.ImportResultMsg:
m.popup = nil
if msg.Result {
return m, app.GetInstance().ImportCollectionFromUrl(msg.Url)
}
return m, cmd
case collections.AddToCollectionResultMsg:
if msg.Result {
cmd = m.AddToCollection()
}
m.popup = nil
return m, cmd
}
// If we are showing a popup, we need to update the popup
if m.popup != nil {
m.popup, cmd = m.popup.Update(msg)
return m, cmd
}
// ----------------------------
switch msg := msg.(type) {
case app.SetFocusMsg:
m.SetFocused(msg.Item)
case app.CollectionEditMsg:
m.popup = collections.NewForm(*msg.Collection, m.GetFadedView(), 70)
return m, m.popup.Init()
case tea.KeyMsg:
{
switch msg.String() {
case "q":
if m.SizeIsTooSmall() {
return m, tea.Quit
}
case "ctrl+c":
if m.SizeIsTooSmall() {
return m, tea.Quit
}
width := 100
m.popup = popup.NewChoice(m.GetFadedView(), width, "Are you sure, you want to quit?", false)
return m, m.popup.Init()
case "ctrl+n":
m.popup = collections.NewForm(app.NewCollection(), m.GetFadedView(), 70)
return m, m.popup.Init()
case "ctrl+o":
m.popup = importer.NewForm(m.GetFadedView(), 70)
return m, m.popup.Init()
case "ctrl+a":
m.popup = NewHelp(m.GetFadedView(), 70)
return m, m.popup.Init()
case "ctrl+s":
url := m.getUrlPane()
coll := collections.NewAddToCollection(m.GetFadedView(), 40, m.tui.LayoutTree.GetWidth())
coll.SetUrl(url.Value())
m.popup = coll
return m, m.popup.Init()
case "tab":
m, cmd := m.Next()
return m, cmd
case "shift+tab":
m, cmd := m.Prev()
return m, cmd
case "ctrl+f":
coll := m.tui.ModelMap["collections"].(collections.Collections)
minified := coll.IsMinified()
m.tui.ModelMap["collections"], cmd = m.tui.ModelMap["collections"].(collections.Collections).SetMinified(!minified)
m.tui.UpdateSize(tea.WindowSizeMsg{Width: m.tui.LayoutTree.GetWidth(), Height: m.tui.LayoutTree.GetHeight()})
if minified && m.focused != "collections" {
return m.SetFocused("collections")
}
if !minified && m.focused == "collections" {
return m.SetFocused("url")
}
return m, nil
default:
if m.focused != "" {
m.tui.ModelMap[m.focused], cmd = m.tui.ModelMap[m.focused].Update(msg)
}
}
}
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
m.tui.UpdateSize(msg)
default:
var cmd tea.Cmd
for key, element := range m.tui.ModelMap {
m.tui.ModelMap[key], cmd = element.Update(msg)
cmds = append(cmds, cmd)
}
}
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m Model) SizeIsTooSmall() bool {
return m.width < 140 || m.height < 30
}
func (m Model) View() string {
if m.SizeIsTooSmall() {
return config.FullscreenStyle.
Width(m.width - 2).
Height(m.height - 2).
Render(
lipgloss.JoinVertical(
lipgloss.Left,
config.BoxHeader.Render("Restman "+version),
"Please resize the window to at least 140x30"),
)
}
if m.popup != nil {
return m.popup.View()
}
return zone.Scan(m.tui.View())
}