-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
143 lines (131 loc) · 3.01 KB
/
commands.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/davecgh/go-spew/spew"
)
func lsImpl() {
fmt.Println(".")
if gCurrentNode.Name != baseAddr {
fmt.Println("..")
}
for _, d := range gCurrentNode.Children {
fmt.Println(d.Name)
}
fmt.Println("")
}
func cdImpl(args []string, root *TreeNode) {
if len(args) == 1 { // no argument -> go to root
gLabel = "/"
gCurrentNode = root
return
}
dest := args[1]
if dest == "." {
return
}
if dest == ".." {
if gCurrentNode.Parent == nil {
return
}
gLabel = gLabel[:strings.LastIndex(gLabel, "/")]
gCurrentNode = gCurrentNode.Parent
if gLabel == "" { // add / when cd to root
gLabel = "/"
}
return
}
label := gLabel
if label[len(label)-1] != '/' {
label += "/"
}
label += dest
for _, d := range gCurrentNode.Children {
if d.Name == dest {
gLabel = label
gCurrentNode = d
return
}
}
msg := fmt.Sprintf("Warning: The '%v' endpoint is not present in the OpenAPI description", label)
fmt.Println("\x1b[33m" + msg + ".\x1b[0m\n")
}
func prettyJSON(body []byte) {
// Create an empty interface to store the unmarshalled JSON data
var parsedJSON interface{}
// Unmarshal the JSON data into the interface
err := json.Unmarshal(body, &parsedJSON)
if err != nil {
fmt.Println("Error:", err)
return
}
// Pretty print the JSON using MarshalIndent
prettyJSON, err := json.MarshalIndent(parsedJSON, "", " ")
if err != nil {
fmt.Println("Error:", err)
return
}
// Print the pretty printed JSON as a string
fmt.Println(string(prettyJSON))
}
func printHeader(resp *http.Response) {
fmt.Println(resp.Status)
for k, v := range resp.Header {
fmt.Printf("%v: %v\n", k, v[0])
}
}
func getImpl(args []string) {
if len(args) != 2 {
fmt.Println("\x1b[31mArgumentCountOutOfRange -- 2\x1b[0m")
fmt.Println("Usage: GET [Options]")
return
}
query := fmt.Sprintf("%s%s/%v", baseAddr, gLabel, args[1])
fmt.Println(query)
resp, err := http.Get(query)
if err != nil {
fmt.Println("\x1b[31mError: Failed to get response.\x1b[0m")
return
} else {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return
}
printHeader(resp)
prettyJSON(body)
}
}
func deleteImpl(args []string) {
if len(args) != 2 {
fmt.Println("\x1b[31mArgumentCountOutOfRange -- 2\x1b[0m\n")
fmt.Println("Usage: DELETE [Options]")
return
}
query := fmt.Sprintf("%s%s/%v", baseAddr, gLabel, args[1])
fmt.Println(query)
req, _ := http.NewRequest("DELETE", query, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Failed sending DELETE request:", err)
return
} else {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return
}
printHeader(resp)
prettyJSON(body)
}
}
func defaultCommand() {
spew.Dump(gLabel)
fmt.Println("\x1b[31mNo matching command found.\x1b[0m")
fmt.Println("\x1b[31mExecute 'help' to see available commands.\x1b[0m\n")
}