@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"fmt"
6
7
"io"
7
8
"log"
@@ -52,13 +53,11 @@ func run(cmd *cobra.Command, args []string) {
52
53
log .Fatalf ("failed to load configuration: %v" , err )
53
54
}
54
55
55
- req , err := http . NewRequestWithContext (ctx , opt . Method , args [0 ], requestBody ( opt . Data ) )
56
+ req , err := request (ctx , args [0 ])
56
57
if err != nil {
57
58
log .Fatalf ("failed to create HTTP request: %v" , err )
58
59
}
59
60
60
- setHeaders (req , opt .Headers )
61
-
62
61
resp , err := sigv4 .NewHTTPClient (& cfg , opt .Service , nil ).Do (req )
63
62
if err != nil {
64
63
log .Fatalf ("failed to HTTP request: %v" , err )
@@ -73,19 +72,24 @@ func run(cmd *cobra.Command, args []string) {
73
72
fmt .Print (string (b ))
74
73
}
75
74
76
- func requestBody (data string ) io.Reader {
77
- if data == "" {
78
- return nil
75
+ func request (ctx context.Context , url string ) (* http.Request , error ) {
76
+ var body io.Reader
77
+ if d := opt .Data ; d != "" {
78
+ body = bytes .NewReader ([]byte (d ))
79
+ }
80
+
81
+ req , err := http .NewRequestWithContext (ctx , opt .Method , url , body )
82
+ if err != nil {
83
+ return nil , err
79
84
}
80
- return bytes .NewReader ([]byte (data ))
81
- }
82
85
83
- func setHeaders (req * http.Request , headers []string ) {
84
- for _ , h := range headers {
86
+ for _ , h := range opt .Headers {
85
87
a := strings .SplitN (h , ":" , 2 )
86
88
if len (a ) != 2 {
87
- continue
89
+ return nil , fmt . Errorf ( "invalid request header [%s]" , h )
88
90
}
89
91
req .Header .Add (strings .TrimSpace (a [0 ]), strings .TrimSpace (a [1 ]))
90
92
}
93
+
94
+ return req , nil
91
95
}
0 commit comments