Skip to content

Commit ec6b5e2

Browse files
committed
Merge pull request #42 from buaazp/master
Support multi print params like '-print=HBhb'
2 parents ffce5be + 149a0e2 commit ec6b5e2

File tree

3 files changed

+73
-35
lines changed

3 files changed

+73
-35
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Submitting forms:
6363

6464
See the request that is being sent using one of the output options:
6565

66-
$ bat -print="H" example.org
66+
$ bat -print="Hhb" example.org
6767

6868
Use Github API to post a comment on an issue with authentication:
6969

bat.go

+71-33
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ import (
3434
"strings"
3535
)
3636

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+
)
3844

3945
var (
4046
ver bool
@@ -45,6 +51,7 @@ var (
4551
auth string
4652
proxy string
4753
printV string
54+
printOption uint8
4855
body string
4956
bench bool
5057
benchN int
@@ -79,7 +86,29 @@ func init() {
7986
jsonmap = make(map[string]interface{})
8087
}
8188

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+
82110
func main() {
111+
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
83112
flag.Usage = usage
84113
flag.Parse()
85114
args := flag.Args()
@@ -90,7 +119,8 @@ func main() {
90119
fmt.Println("Version:", version)
91120
os.Exit(2)
92121
}
93-
if printV != "A" && printV != "B" {
122+
parsePrintOption(printV)
123+
if printOption&printReqBody != printReqBody {
94124
defaultSetting.DumpBody = false
95125
}
96126
var stdin []byte
@@ -241,28 +271,32 @@ func main() {
241271
panic(err)
242272
}
243273
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
254282
}
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))
256290
fmt.Println("")
257291
}
258-
if printV == "A" || printV == "h" {
292+
if printOption&printRespHeader == printRespHeader {
259293
fmt.Println(Color(res.Proto, Magenta), Color(res.Status, Green))
260294
for k, v := range res.Header {
261295
fmt.Println(Color(k, Gray), ":", Color(strings.Join(v, " "), Cyan))
262296
}
263297
fmt.Println("")
264298
}
265-
if printV == "A" || printV == "b" {
299+
if printOption&printRespBody == printRespBody {
266300
body := formatResponseBody(res, httpreq, pretty)
267301
fmt.Println(ColorfulResponse(body, res.Header.Get("Content-Type")))
268302
}
@@ -274,28 +308,32 @@ func main() {
274308
}
275309
}
276310
} 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
287319
}
288-
fmt.Println(string(dump))
320+
}
321+
if printOption&printReqHeader == printReqHeader {
322+
fmt.Println(string(dumpHeader))
289323
fmt.Println("")
290324
}
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 {
292330
fmt.Println(res.Proto, res.Status)
293331
for k, v := range res.Header {
294332
fmt.Println(k, ":", strings.Join(v, " "))
295333
}
296334
fmt.Println("")
297335
}
298-
if printV == "A" || printV == "b" {
336+
if printOption&printRespBody == printRespBody {
299337
body := formatResponseBody(res, httpreq, pretty)
300338
fmt.Println(body)
301339
}
@@ -307,7 +345,7 @@ var usageinfo string = `bat is a Go implemented CLI cURL-like tool for humans.
307345
Usage:
308346
309347
bat [flags] [METHOD] URL [ITEM [ITEM]]
310-
348+
311349
flags:
312350
-a, -auth=USER[:PASS] Pass a username:password pair as the argument
313351
-b, -bench=false Sends bench requests to URL
@@ -324,7 +362,7 @@ flags:
324362
"B" request body
325363
"h" response headers
326364
"b" response body
327-
-v, -version=true Show Version Number
365+
-v, -version=true Show Version Number
328366
329367
METHOD:
330368
bat defaults to either GET (if there is no request data) or POST (with request data).
@@ -341,10 +379,10 @@ ITEM:
341379
File upload key@/path/file
342380
343381
Example:
344-
382+
345383
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
348386
`
349387

350388
func usage() {

color.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func ColorStart(color uint8) string {
2828

2929
func ColorfulRequest(str string) string {
3030
lines := strings.Split(str, "\n")
31-
if printV == "A" || printV == "H" {
31+
if printOption&printReqHeader == printReqHeader {
3232
strs := strings.Split(lines[0], " ")
3333
strs[0] = Color(strs[0], Magenta)
3434
strs[1] = Color(strs[1], Cyan)

0 commit comments

Comments
 (0)