@@ -34,7 +34,13 @@ import (
34
34
"strings"
35
35
)
36
36
37
- const version = "0.1.0"
37
+ const (
38
+ version = "0.1.0"
39
+ printReqHeader uint8 = 1 << (iota - 1 )
40
+ printReqBody
41
+ printRespHeader
42
+ printRespBody
43
+ )
38
44
39
45
var (
40
46
ver bool
45
51
auth string
46
52
proxy string
47
53
printV string
54
+ printOption uint8
48
55
body string
49
56
bench bool
50
57
benchN int
@@ -79,7 +86,29 @@ func init() {
79
86
jsonmap = make (map [string ]interface {})
80
87
}
81
88
89
+ func parsePrintOption (s string ) {
90
+ if strings .ContainsRune (s , 'A' ) {
91
+ printOption = printReqHeader | printReqBody | printRespHeader | printRespBody
92
+ return
93
+ }
94
+
95
+ if strings .ContainsRune (s , 'H' ) {
96
+ printOption |= printReqHeader
97
+ }
98
+ if strings .ContainsRune (s , 'B' ) {
99
+ printOption |= printReqBody
100
+ }
101
+ if strings .ContainsRune (s , 'h' ) {
102
+ printOption |= printRespHeader
103
+ }
104
+ if strings .ContainsRune (s , 'b' ) {
105
+ printOption |= printRespBody
106
+ }
107
+ return
108
+ }
109
+
82
110
func main () {
111
+ log .SetFlags (log .LstdFlags | log .Lshortfile | log .Lmicroseconds )
83
112
flag .Usage = usage
84
113
flag .Parse ()
85
114
args := flag .Args ()
@@ -90,7 +119,8 @@ func main() {
90
119
fmt .Println ("Version:" , version )
91
120
os .Exit (2 )
92
121
}
93
- if printV != "A" && printV != "B" {
122
+ parsePrintOption (printV )
123
+ if printOption & printReqBody != printReqBody {
94
124
defaultSetting .DumpBody = false
95
125
}
96
126
var stdin []byte
@@ -241,28 +271,32 @@ func main() {
241
271
panic (err )
242
272
}
243
273
if fi .Mode ()& os .ModeDevice == os .ModeDevice {
244
- if printV == "A" || printV == "H" || printV == "B" {
245
- dump := httpreq .DumpRequest ()
246
- if printV == "B" {
247
- dps := strings .Split (string (dump ), "\n " )
248
- for i , line := range dps {
249
- if len (strings .Trim (line , "\r \n " )) == 0 {
250
- dump = []byte (strings .Join (dps [i :], "\n " ))
251
- break
252
- }
253
- }
274
+ var dumpHeader , dumpBody []byte
275
+ dump := httpreq .DumpRequest ()
276
+ dps := strings .Split (string (dump ), "\n " )
277
+ for i , line := range dps {
278
+ if len (strings .Trim (line , "\r \n " )) == 0 {
279
+ dumpHeader = []byte (strings .Join (dps [:i ], "\n " ))
280
+ dumpBody = []byte (strings .Join (dps [i :], "\n " ))
281
+ break
254
282
}
255
- fmt .Println (ColorfulRequest (string (dump )))
283
+ }
284
+ if printOption & printReqHeader == printReqHeader {
285
+ fmt .Println (ColorfulRequest (string (dumpHeader )))
286
+ fmt .Println ("" )
287
+ }
288
+ if printOption & printReqBody == printReqBody {
289
+ fmt .Println (string (dumpBody ))
256
290
fmt .Println ("" )
257
291
}
258
- if printV == "A" || printV == "h" {
292
+ if printOption & printRespHeader == printRespHeader {
259
293
fmt .Println (Color (res .Proto , Magenta ), Color (res .Status , Green ))
260
294
for k , v := range res .Header {
261
295
fmt .Println (Color (k , Gray ), ":" , Color (strings .Join (v , " " ), Cyan ))
262
296
}
263
297
fmt .Println ("" )
264
298
}
265
- if printV == "A" || printV == "b" {
299
+ if printOption & printRespBody == printRespBody {
266
300
body := formatResponseBody (res , httpreq , pretty )
267
301
fmt .Println (ColorfulResponse (body , res .Header .Get ("Content-Type" )))
268
302
}
@@ -274,28 +308,32 @@ func main() {
274
308
}
275
309
}
276
310
} else {
277
- if printV == "A" || printV == "H" || printV == "B" {
278
- dump := httpreq .DumpRequest ()
279
- if printV == "B" {
280
- dps := strings .Split (string (dump ), "\n " )
281
- for i , line := range dps {
282
- if len (strings .Trim (line , "\r \n " )) == 0 {
283
- dump = []byte (strings .Join (dps [i :], "\n " ))
284
- break
285
- }
286
- }
311
+ var dumpHeader , dumpBody []byte
312
+ dump := httpreq .DumpRequest ()
313
+ dps := strings .Split (string (dump ), "\n " )
314
+ for i , line := range dps {
315
+ if len (strings .Trim (line , "\r \n " )) == 0 {
316
+ dumpHeader = []byte (strings .Join (dps [:i ], "\n " ))
317
+ dumpBody = []byte (strings .Join (dps [i :], "\n " ))
318
+ break
287
319
}
288
- fmt .Println (string (dump ))
320
+ }
321
+ if printOption & printReqHeader == printReqHeader {
322
+ fmt .Println (string (dumpHeader ))
289
323
fmt .Println ("" )
290
324
}
291
- if printV == "A" || printV == "h" {
325
+ if printOption & printReqBody == printReqBody {
326
+ fmt .Println (string (dumpBody ))
327
+ fmt .Println ("" )
328
+ }
329
+ if printOption & printRespHeader == printRespHeader {
292
330
fmt .Println (res .Proto , res .Status )
293
331
for k , v := range res .Header {
294
332
fmt .Println (k , ":" , strings .Join (v , " " ))
295
333
}
296
334
fmt .Println ("" )
297
335
}
298
- if printV == "A" || printV == "b" {
336
+ if printOption & printRespBody == printRespBody {
299
337
body := formatResponseBody (res , httpreq , pretty )
300
338
fmt .Println (body )
301
339
}
@@ -307,7 +345,7 @@ var usageinfo string = `bat is a Go implemented CLI cURL-like tool for humans.
307
345
Usage:
308
346
309
347
bat [flags] [METHOD] URL [ITEM [ITEM]]
310
-
348
+
311
349
flags:
312
350
-a, -auth=USER[:PASS] Pass a username:password pair as the argument
313
351
-b, -bench=false Sends bench requests to URL
@@ -324,7 +362,7 @@ flags:
324
362
"B" request body
325
363
"h" response headers
326
364
"b" response body
327
- -v, -version=true Show Version Number
365
+ -v, -version=true Show Version Number
328
366
329
367
METHOD:
330
368
bat defaults to either GET (if there is no request data) or POST (with request data).
@@ -341,10 +379,10 @@ ITEM:
341
379
File upload key@/path/file
342
380
343
381
Example:
344
-
382
+
345
383
bat beego.me
346
-
347
- more help information please refer to https://github.com/astaxie/bat
384
+
385
+ more help information please refer to https://github.com/astaxie/bat
348
386
`
349
387
350
388
func usage () {
0 commit comments