-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmds.go
217 lines (189 loc) · 5.63 KB
/
cmds.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
package main
import (
"errors"
"fmt"
neturl "net/url"
"os"
"restman/app"
"restman/components/collections"
"restman/components/config"
"restman/components/footer"
"restman/components/request"
"restman/components/results"
"restman/components/url"
"restman/utils"
"strings"
tea "github.com/charmbracelet/bubbletea"
zone "github.com/lrstanley/bubblezone"
"github.com/spf13/cobra"
"github.com/spf13/viper"
boxer "github.com/treilik/bubbleboxer"
)
var version = "dev"
var rootCmd = &cobra.Command{
Use: "restman [http://example.com/api/v1]",
Short: "A CLI tool for RESTful API",
Long: `
┏┓┏┓┏╋┏┳┓┏┓┏┓
┛ ┗ ┛┗┛┗┗┗┻┛┗
Restman is a CLI tool for RESTful API.`,
Version: version,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("accepts only one optional URL")
}
if len(args) == 1 {
u, err := neturl.Parse(args[0])
if err != nil || u.Scheme == "" || u.Host == "" {
return errors.New("invalid URL provided")
}
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
config.SetVersion(version)
call := app.NewCall()
if len(args) >= 1 {
call.Url = args[0]
}
// TODO: what if both args and flags are provided?
curl, _ := cmd.Flags().GetString("url")
if curl != "" {
call.Url = curl
}
method, _ := cmd.Flags().GetString("request")
if method != "" {
call.Method = method
}
data, _ := cmd.Flags().GetString("data")
dataRaw, _ := cmd.Flags().GetString("data-raw")
call.Data = data
if call.Data == "" {
call.Data = dataRaw
}
if call.Data != "" {
call.DataType = "Text"
}
// make sure the method is POST if data is provided
if call.Data != "" && call.Method == "GET" {
call.Method = "POST"
}
headers, _ := cmd.Flags().GetStringArray("header")
if headers != nil {
// split headers into key-value pairs
// to check authorization for bearer token
processed_headers := []string{}
for _, h := range headers {
if h == "" {
continue
}
pair := strings.Split(h, ":")
if len(pair) == 2 {
if strings.ToLower(pair[0]) == "authorization" && strings.Contains(pair[1], "Bearer") {
call.Auth = &app.Auth{Type: "bearer_token", Token: strings.TrimSpace(strings.ReplaceAll(pair[1], "Bearer", ""))}
continue
}
if strings.ToLower(pair[0]) == "content-type" && strings.Contains(pair[1], "application/json") {
call.DataType = "JSON"
call.Data = utils.FormatJSON(call.Data)
}
}
processed_headers = append(processed_headers, h)
}
call.Headers = processed_headers
}
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("json") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("/etc/restman/") // path to look for the config file in
viper.AddConfigPath("$HOME/.restman") // call multiple times to add many search paths
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// NOTE: ignore if config file is not found
} else {
panic(fmt.Errorf("fatal error config file: %w", err))
}
}
var default_headers map[string]string = viper.GetStringMapString("default_headers")
for k, v := range default_headers {
call.Headers = append(call.Headers, fmt.Sprintf("%s: %s", k, v))
}
// ----
zone.NewGlobal()
// layout-tree defintion
m := Model{tui: boxer.Boxer{}, focused: "url", initialCall: call}
url := url.New()
resultsBox := results.New()
requestBox := request.New()
footerBox := footer.New()
colBox := collections.New()
splitNode := boxer.CreateNoBorderNode()
splitNode.SizeFunc = func(node boxer.Node, widthOrHeight int) []int {
paramsSize := int(float64(widthOrHeight) * 0.4)
return []int{
paramsSize,
widthOrHeight - paramsSize,
}
}
splitNode.Children = []boxer.Node{
stripErr(m.tui.CreateLeaf("request", requestBox)),
stripErr(m.tui.CreateLeaf("results", resultsBox)),
}
centerNode := boxer.CreateNoBorderNode()
centerNode.VerticalStacked = true
centerNode.SizeFunc = func(node boxer.Node, widthOrHeight int) []int {
return []int{
3,
widthOrHeight - 3,
}
}
centerNode.Children = []boxer.Node{
stripErr(m.tui.CreateLeaf("url", url)),
splitNode,
}
// middle Node
middleNode := boxer.CreateNoBorderNode()
middleNode.SizeFunc = func(node boxer.Node, widthOrHeight int) []int {
gap := widthOrHeight / 5
if m.tui.ModelMap["collections"].(collections.Collections).IsMinified() {
gap = 6
}
return []int{
gap,
widthOrHeight - gap,
}
}
middleNode.Children = []boxer.Node{
stripErr(m.tui.CreateLeaf("collections", colBox)),
centerNode,
}
rootNode := boxer.CreateNoBorderNode()
rootNode.VerticalStacked = true
rootNode.SizeFunc = func(node boxer.Node, widthOrHeight int) []int {
return []int{
widthOrHeight - 1,
1,
}
}
rootNode.Children = []boxer.Node{
middleNode,
stripErr(m.tui.CreateLeaf("footer", footerBox)),
}
m.tui.LayoutTree = rootNode
if f, err := tea.LogToFile("debug.log", "debug"); err != nil {
fmt.Println("Couldn't open a file for logging:", err)
os.Exit(1)
} else {
defer f.Close()
}
p := tea.NewProgram(
m,
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
)
if _, err := p.Run(); err != nil {
fmt.Println("could not run program:", err)
os.Exit(1)
}
},
}