DownloadFile downloads the given SQL file to the destination.
(ctx context.Context, hc *http.Client, item Item, rawURL string, dest string)
| 183 | |
| 184 | // DownloadFile downloads the given SQL file to the destination. |
| 185 | func DownloadFile(ctx context.Context, hc *http.Client, item Item, rawURL string, dest string) error { |
| 186 | req, _ := http.NewRequestWithContext(ctx, "GET", rawURL, nil) |
| 187 | req.Header.Set("User-Agent", "gh-pg-dump-finder/1.0") |
| 188 | resp, err := hc.Do(req) |
| 189 | if err != nil { |
| 190 | return err |
| 191 | } |
| 192 | defer resp.Body.Close() |
| 193 | if resp.StatusCode != 200 { |
| 194 | return fmt.Errorf("download HTTP %d", resp.StatusCode) |
| 195 | } |
| 196 | out, err := os.Create(dest) |
| 197 | if err != nil { |
| 198 | return err |
| 199 | } |
| 200 | defer out.Close() |
| 201 | _, _ = io.WriteString(out, fmt.Sprintf("-- Downloaded from: %s\n", item.HtmlURL)) |
| 202 | _, err = io.Copy(out, resp.Body) |
| 203 | return err |
| 204 | } |
| 205 | |
| 206 | // SetHeaders sets the appropriate headers for a request. |
| 207 | func SetHeaders(req *http.Request, token string) { |