-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (96 loc) · 1.97 KB
/
Copy pathmain.go
File metadata and controls
103 lines (96 loc) · 1.97 KB
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
package main
import (
"fmt"
"os"
"strings"
"github.com/manifoldco/promptui"
)
var (
baseAddr, gLabel string
gCurrentNode *TreeNode // current working node
openapiOps = []string{"delete", "list", "get", "create", "update", "condition"}
)
func selectTest() []string {
prompt := promptui.Prompt{
Label: baseAddr + gLabel,
}
result, err := prompt.Run()
if err == promptui.ErrInterrupt || err == promptui.ErrEOF {
fmt.Println("⏎ ")
os.Exit(0)
}
if result == "" {
return nil
}
args, err := splitArgs(result)
if err != nil {
fmt.Printf("\x1b[31mError parsing command: %v\x1b[0m\n", err)
return nil
}
return args
}
func startupURL(opts argsOptions, root *TreeNode) {
if opts.startURL == "" {
return
}
url := opts.startURL
if url[0] != '/' {
url = "/" + url
}
gLabel = url
path := strings.Split(gLabel, "/")
for _, p := range path {
if p == "" {
continue
}
gCurrentNode = gCurrentNode.Children[p]
if gCurrentNode == nil {
gLabel = "/"
gCurrentNode = root
return
}
}
}
func main() {
opts := initOptions()
baseAddr = opts.baseAddr
gLabel = "/"
root := buildTree(baseAddr, opts.openapiPath)
gCurrentNode = root
startupURL(opts, root)
mainloop:
for {
result := selectTest()
if result == nil {
continue
}
switch strings.ToLower(result[0]) {
case "ls":
lsImpl()
case "cd":
cdImpl(result, root)
case "get", "post", "put", "delete", "patch", "head", "options":
makeRequest(strings.ToUpper(result[0]), result)
case "set":
if len(result) >= 2 && result[1] == "header" {
handleHeaderCommand(result)
} else {
fmt.Println("Unknown set command. Use 'set header <name> <value>'")
}
case "clear":
if len(result) >= 2 && result[1] == "header" {
handleClearCommand(result)
} else {
fmt.Println("Unknown clear command. Use 'clear header <name>'")
}
case "tree":
printTree(root, 0)
case "help":
helpImpl()
case "exit":
break mainloop
default:
defaultCommand()
}
}
}