-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (164 loc) · 3.79 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/nxtcoder17/http-cli/pkg/parser"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
var debug = false
func showOutput(label string, msg any) {
fmt.Printf("\n# %s\n", label)
switch t := msg.(type) {
case io.ReadCloser:
b, err := io.ReadAll(t)
if err != nil {
panic(err)
}
nb := new(bytes.Buffer)
if err := json.Indent(nb, b, "", " "); err != nil {
fmt.Println(errors.Wrapf(err, "[ERROR]: failed to decode response body into json format"))
fmt.Print("\n### RAW body:\n")
fmt.Printf("```\n%s\n```\n", b)
return
}
fmt.Printf("%s\n", nb.String())
case []byte:
fmt.Printf("%s\n", t)
case string:
fmt.Printf("%s\n", t)
case http.Header:
for k, v := range t {
fmt.Printf("%-20s: %-30s\n", k, strings.Join(v, ","))
}
default:
b, err := json.MarshalIndent(msg, "", " ")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s\n", b)
}
}
var flags = []cli.Flag{
&cli.StringFlag{
Name: "file",
Required: true,
Usage: "filename with yaml queries",
},
&cli.StringFlag{
Name: "envFile",
Required: true,
Usage: "gqlenv file",
},
&cli.UintFlag{
Name: "lineNo",
Required: true,
Usage: "lineNo for yaml block to be executed",
},
}
func main() {
app := cli.NewApp()
app.Name = "http-cli"
app.Commands = []*cli.Command{
{
Name: "graphql",
Aliases: []string{"g", "gq", "gql"},
Usage: "graphql query",
Flags: flags,
Action: func(cctx *cli.Context) error {
yamlFile := cctx.String("file")
lineNo := cctx.Uint("lineNo")
envFile := cctx.String("envFile")
queryBlock, err := parser.ReadQueryFile(yamlFile, lineNo)
if err != nil {
return err
}
if debug {
showOutput("raw query", string(queryBlock.YAMLQuery))
}
ef, err := parser.ParseEnvFile(envFile)
if err != nil {
return err
}
if debug {
showOutput("Parsed Env", *ef)
}
req, err := parser.ParseGqlQuery(queryBlock, ef)
if err != nil {
return err
}
showOutput("request", map[string]any{
"url": req.URL.String(),
"headers": req.Header,
})
start := time.Now()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
d := time.Since(start)
showOutput("time taken", fmt.Sprintf("%.2fs", d.Seconds()))
showOutput("response headers", resp.Header)
showOutput("response body\n```json", resp.Body)
fmt.Println("```")
return nil
},
},
{
Name: "rest",
Aliases: []string{"r", "rest"},
Usage: "rest api calls",
Flags: flags,
Action: func(cctx *cli.Context) error {
yamlFile := cctx.String("file")
lineNo := cctx.Uint("lineNo")
envFile := cctx.String("envFile")
queryBlock, err := parser.ReadQueryFile(yamlFile, lineNo)
if err != nil {
return err
}
if debug {
showOutput("global block", queryBlock.Global)
showOutput("query", string(queryBlock.YAMLQuery))
}
ef, err := parser.ParseEnvFile(envFile)
if err != nil {
return err
}
if debug {
showOutput("env file", ef)
}
req, err := parser.ParseRestQuery(queryBlock, ef)
if err != nil {
return err
}
showOutput("request", map[string]any{
"url": req.URL.String(),
"headers": req.Header,
})
start := time.Now()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
d := time.Since(start)
showOutput("time taken", fmt.Sprintf("%.2fs", d.Seconds()))
showOutput("response headers", resp.Header)
showOutput("response status", resp.Status)
showOutput("response body", resp.Body)
return nil
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}