Skip to content

Commit e152bed

Browse files
committed
Merge pull request astaxie#8 from glkz/refactor_response_formatter
Refactor duplicated response-body-formatter code
2 parents 6493911 + a69a044 commit e152bed

File tree

1 file changed

+32
-51
lines changed

1 file changed

+32
-51
lines changed

bat.go

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"os"
2828
"runtime"
2929
"strings"
30+
31+
"github.com/astaxie/bat/httplib"
3032
)
3133

3234
const version = "0.0.1"
@@ -57,6 +59,28 @@ func init() {
5759
jsonmap = make(map[string]interface{})
5860
}
5961

62+
func formatResponseBody(res *http.Response, httpreq *httplib.BeegoHttpRequest, pretty bool) string {
63+
str, err := httpreq.String()
64+
if err != nil {
65+
log.Fatalln("can't get the url", err)
66+
}
67+
fmt.Println("")
68+
if pretty && strings.Contains(res.Header.Get("Content-Type"), contentJsonRegex) {
69+
var output interface{}
70+
err = json.Unmarshal([]byte(str), &output)
71+
if err != nil {
72+
log.Fatal("Response Json Unmarshal", err)
73+
}
74+
bstr, err := json.MarshalIndent(output, "", " ")
75+
if err != nil {
76+
log.Fatal("Response Json MarshalIndent", err)
77+
}
78+
str = string(bstr)
79+
}
80+
81+
return str
82+
}
83+
6084
func main() {
6185
flag.Usage = usage
6286
flag.Parse()
@@ -155,42 +179,13 @@ func main() {
155179
for k, v := range res.Header {
156180
fmt.Println(Color(k, Gray), ":", Color(strings.Join(v, " "), Cyan))
157181
}
158-
str, err := httpreq.String()
159-
if err != nil {
160-
log.Fatalln("can't get the url", err)
161-
}
162182
fmt.Println("")
163-
if pretty && strings.Contains(res.Header.Get("Content-Type"), contentJsonRegex) {
164-
var output interface{}
165-
err = json.Unmarshal([]byte(str), &output)
166-
if err != nil {
167-
log.Fatal("Response Json Unmarshal", err)
168-
}
169-
bstr, err := json.MarshalIndent(output, "", " ")
170-
if err != nil {
171-
log.Fatal("Response Json MarshalIndent", err)
172-
}
173-
str = string(bstr)
174-
}
175-
fmt.Println(ColorfulResponse(str))
183+
184+
body := formatResponseBody(res, httpreq, pretty)
185+
fmt.Println(ColorfulResponse(body))
176186
} else {
177-
str, err := httpreq.String()
178-
if err != nil {
179-
log.Fatalln("can't get the url", err)
180-
}
181-
if pretty && strings.Contains(res.Header.Get("Content-Type"), contentJsonRegex) {
182-
var output interface{}
183-
err = json.Unmarshal([]byte(str), &output)
184-
if err != nil {
185-
log.Fatal("Response Json Unmarshal", err)
186-
}
187-
bstr, err := json.MarshalIndent(output, "", " ")
188-
if err != nil {
189-
log.Fatal("Response Json MarshalIndent", err)
190-
}
191-
str = string(bstr)
192-
}
193-
_, err = os.Stdout.WriteString(str)
187+
body := formatResponseBody(res, httpreq, pretty)
188+
_, err = os.Stdout.WriteString(body)
194189
if err != nil {
195190
log.Fatal(err)
196191
}
@@ -203,24 +198,10 @@ func main() {
203198
for k, v := range res.Header {
204199
fmt.Println(k, ":", strings.Join(v, " "))
205200
}
206-
str, err := httpreq.String()
207-
if err != nil {
208-
log.Fatalln("can't get the url", err)
209-
}
210201
fmt.Println("")
211-
if pretty && strings.Contains(res.Header.Get("Content-Type"), contentJsonRegex) {
212-
var output interface{}
213-
err = json.Unmarshal([]byte(str), &output)
214-
if err != nil {
215-
log.Fatal("Response Json Unmarshal", err)
216-
}
217-
bstr, err := json.MarshalIndent(output, "", " ")
218-
if err != nil {
219-
log.Fatal("Response Json MarshalIndent", err)
220-
}
221-
str = string(bstr)
222-
}
223-
fmt.Println(str)
202+
203+
body := formatResponseBody(res, httpreq, pretty)
204+
fmt.Println(body)
224205
}
225206
}
226207

0 commit comments

Comments
 (0)