|
1 | 1 | package commands |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "errors" |
6 | 7 | "fmt" |
@@ -103,7 +104,7 @@ func validateConfiguration(schemaPath, targetFile string) error { |
103 | 104 | compiler := jsonschema.NewCompiler() |
104 | 105 | loader := jsonschema.SchemeURLLoader{ |
105 | 106 | "file": jsonschema.FileLoader{}, |
106 | | - "https": newHTTPURLLoader(), |
| 107 | + "https": newJSONSchemaHTTPLoader(), |
107 | 108 | } |
108 | 109 | compiler.UseLoader(loader) |
109 | 110 | compiler.DefaultDraft(jsonschema.Draft7) |
@@ -182,22 +183,29 @@ func decodeTomlFile(filename string) (any, error) { |
182 | 183 | return m, nil |
183 | 184 | } |
184 | 185 |
|
185 | | -type httpURLLoader http.Client |
| 186 | +type jsonschemaHTTPLoader struct { |
| 187 | + *http.Client |
| 188 | +} |
186 | 189 |
|
187 | | -func newHTTPURLLoader() *httpURLLoader { |
188 | | - httpLoader := httpURLLoader(http.Client{ |
| 190 | +func newJSONSchemaHTTPLoader() *jsonschemaHTTPLoader { |
| 191 | + return &jsonschemaHTTPLoader{Client: &http.Client{ |
189 | 192 | Timeout: 2 * time.Second, |
190 | | - }) |
191 | | - return &httpLoader |
| 193 | + }} |
192 | 194 | } |
193 | 195 |
|
194 | | -func (l *httpURLLoader) Load(url string) (any, error) { |
195 | | - client := (*http.Client)(l) |
196 | | - resp, err := client.Get(url) |
| 196 | +func (l jsonschemaHTTPLoader) Load(url string) (any, error) { |
| 197 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, http.NoBody) |
197 | 198 | if err != nil { |
198 | 199 | return nil, err |
199 | 200 | } |
| 201 | + |
| 202 | + resp, err := l.Do(req) |
| 203 | + if err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + |
200 | 207 | defer resp.Body.Close() |
| 208 | + |
201 | 209 | if resp.StatusCode != http.StatusOK { |
202 | 210 | return nil, fmt.Errorf("%s returned status code %d", url, resp.StatusCode) |
203 | 211 | } |
|
0 commit comments