-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
102 lines (93 loc) · 1.96 KB
/
request.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
package wizard
import (
"encoding/json"
"fmt"
"github.com/atotto/clipboard"
"github.com/cjp2600/cutos/log"
"github.com/manifoldco/promptui"
"strings"
)
type Request struct {
Description string
Tag string
Request string
RequestValidation func(input string) error
Response string
}
func NewRequest() *Request {
return &Request{}
}
func (w *Request) SetRequestValidation(rv func(input string) error) {
w.RequestValidation = rv
}
// SetTag
func (w *Request) SetTag() *Request {
title := "Tag"
if len(w.Tag) > 0 {
title = title + " (...)"
}
prompt := promptui.Prompt{
Label: title,
}
result, err := prompt.Run()
if err != nil {
log.Fatal(err)
}
if len(result) > 0 {
w.Tag = strings.ToLower(result)
}
return w
}
// SetDescription
func (w *Request) SetDescription() *Request {
title := "Description"
if len(w.Description) > 0 {
title = title + " (...)"
}
prompt := promptui.Prompt{
Label: title,
}
result, err := prompt.Run()
if err != nil {
log.Fatal(err)
}
if len(result) > 0 {
w.Description = result
}
return w
}
// SetRequest
func (w *Request) SetRequest(p string) *Request {
fmt.Printf("%s Сopy data on your clipboard ... \n", promptui.Styler(promptui.FGYellow)("REQUEST ▶"))
var text string
for {
if strings.Contains(text, p) {
break
}
text, _ = clipboard.ReadAll()
}
w.Request = text
fmt.Printf("%s copied \n", promptui.Styler(promptui.FGGreen)("✔"))
return w
}
func (w *Request) isJSON(s string) bool {
var j map[string]interface{}
if err := json.Unmarshal([]byte(s), &j); err != nil {
return false
}
return true
}
// SetResponse
func (w *Request) SetResponse() *Request {
fmt.Printf("%s Сopy json response on your clipboard ... \n", promptui.Styler(promptui.FGYellow)("RESPONSE ▶"))
var text string
for {
if w.isJSON(text) {
break
}
text, _ = clipboard.ReadAll()
}
w.Response = text
fmt.Printf("%s copied \n", promptui.Styler(promptui.FGGreen)("✔"))
return w
}