Skip to content

Commit ddb5cdf

Browse files
committed
Make request and respond more uniform
1 parent 571b23b commit ddb5cdf

File tree

3 files changed

+17
-34
lines changed

3 files changed

+17
-34
lines changed

cmd/request/request.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,31 @@ import (
1111
)
1212

1313
var headersIncluded bool
14-
var outputHeaders bool
15-
var outputStatus bool
14+
var outputResponse bool
1615
var bodyFn string
1716

1817
func init() {
1918
Cmd.Flags().BoolVarP(&headersIncluded, "include-headers", "i", false, "Read headers from the request body")
20-
Cmd.Flags().BoolVarP(&outputHeaders, "output-headers", "o", false, "Output response headers")
21-
Cmd.Flags().BoolVarP(&outputStatus, "output-status", "s", false, "Output response status line")
19+
Cmd.Flags().BoolVarP(&outputResponse, "output-response", "o", false, "Output response status line and headers")
2220
Cmd.Flags().StringVarP(&bodyFn, "body", "b", "", "Filename to read the request body from. Use - for stdin.")
2321
}
2422

2523
var Cmd = &cobra.Command{
2624
Use: "request [method] [url]",
27-
Short: "Send a web request to a url",
25+
Short: "Send a web request to a url and print the response",
2826
Aliases: []string{"get", "post", "put", "delete"},
2927
Args: cobra.RangeArgs(1, 2),
3028
Run: func(cmd *cobra.Command, args []string) {
3129
var method, url string
3230

3331
if cmd.CalledAs() == "request" {
34-
if len(args) != 2 {
35-
cmd.Help()
36-
os.Exit(1)
32+
if len(args) == 1 {
33+
method = "get"
34+
url = args[0]
35+
} else {
36+
method = args[0]
37+
url = args[1]
3738
}
38-
39-
method = args[0]
40-
url = args[1]
4139
} else {
4240
if len(args) != 1 {
4341
cmd.Help()
@@ -67,6 +65,6 @@ var Cmd = &cobra.Command{
6765
resp, err := internal.MakeRequest(method, url, input, headersIncluded)
6866
cobra.CheckErr(err)
6967

70-
cobra.CheckErr(internal.PrintResponse(resp, outputHeaders, outputStatus))
68+
cobra.CheckErr(internal.PrintResponse(resp, outputResponse))
7169
},
7270
}

cmd/respond/respond.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616

1717
var headersIncluded bool
1818
var includeHeaders bool
19-
var includeMethod bool
20-
var includeUrl bool
2119
var bodyFn string
2220
var address string
2321
var port int
@@ -26,8 +24,6 @@ var status int
2624
func init() {
2725
Cmd.Flags().BoolVarP(&headersIncluded, "include-headers", "i", false, "Read headers from the response body")
2826
Cmd.Flags().BoolVarP(&includeHeaders, "output-headers", "o", false, "Output request headers")
29-
Cmd.Flags().BoolVarP(&includeMethod, "output-method", "m", false, "Output request method")
30-
Cmd.Flags().BoolVarP(&includeUrl, "output-url", "u", false, "Output request URL")
3127
Cmd.Flags().StringVarP(&bodyFn, "body", "b", "", "Filename to read the response body from. Use - or omit for stdin")
3228
Cmd.Flags().StringVarP(&address, "address", "a", "", "Address to listen on")
3329
Cmd.Flags().IntVarP(&port, "port", "p", 8000, "Port to listen on")
@@ -47,8 +43,6 @@ var Cmd = &cobra.Command{
4743
handler := responder{
4844
status: status,
4945
includeHeaders: includeHeaders,
50-
includeMethod: includeMethod,
51-
includeUrl: includeUrl,
5246
headersIncluded: headersIncluded,
5347
}
5448

@@ -74,6 +68,8 @@ var Cmd = &cobra.Command{
7468
}
7569
}()
7670

71+
fmt.Println("Listening on", address)
72+
7773
<-ch
7874

7975
server.Shutdown(context.Background())
@@ -85,14 +81,12 @@ var ch chan bool
8581
type responder struct {
8682
status int
8783
includeHeaders bool
88-
includeMethod bool
89-
includeUrl bool
9084
headersIncluded bool
9185
data io.Reader
9286
}
9387

9488
func (h responder) ServeHTTP(w http.ResponseWriter, req *http.Request) {
95-
cobra.CheckErr(internal.PrintRequest(req, h.includeMethod, h.includeUrl, h.includeHeaders))
89+
cobra.CheckErr(internal.PrintRequest(req, h.includeHeaders))
9690

9791
inputReader := bufio.NewReader(h.data)
9892

internal/web.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,14 @@ func MakeRequest(method string, url string, input io.Reader, headersIncluded boo
5656
}
5757

5858
// PrintRequest writes an http.Request to stdout
59-
func PrintRequest(req *http.Request, includeMethod bool, includeUrl bool, includeHeaders bool) error {
59+
func PrintRequest(req *http.Request, includeHeaders bool) error {
6060
body, err := ioutil.ReadAll(req.Body)
6161
req.Body.Close()
6262
if err != nil {
6363
return err
6464
}
6565

66-
if includeMethod {
67-
fmt.Println(req.Method)
68-
}
69-
70-
if includeUrl {
71-
fmt.Println(req.URL)
72-
}
66+
fmt.Printf("%s %s\n", req.Method, req.URL)
7367

7468
if includeHeaders {
7569
req.Header.Write(os.Stdout)
@@ -82,19 +76,16 @@ func PrintRequest(req *http.Request, includeMethod bool, includeUrl bool, includ
8276
}
8377

8478
// PrintResponse writes an http.Response to stdout
85-
func PrintResponse(resp *http.Response, includeHeaders bool, includeStatus bool) error {
79+
func PrintResponse(resp *http.Response, includeHeaders bool) error {
8680
body, err := ioutil.ReadAll(resp.Body)
8781
resp.Body.Close()
88-
8982
if err != nil {
9083
return err
9184
}
9285

93-
if includeStatus {
86+
if includeHeaders {
9487
fmt.Println(resp.Status)
95-
}
9688

97-
if includeHeaders {
9889
resp.Header.Write(os.Stdout)
9990
fmt.Println()
10091
}

0 commit comments

Comments
 (0)