downloadFile downloads a file from a URL to a temporary file and returns its name.
(url string)
| 505 | |
| 506 | // downloadFile downloads a file from a URL to a temporary file and returns its name. |
| 507 | func downloadFile(url string) (string, error) { |
| 508 | client := &http.Client{Timeout: 15 * time.Second} |
| 509 | |
| 510 | resp, err := client.Get(url) |
| 511 | if err != nil { |
| 512 | return "", fmt.Errorf("failed to download %s: %w", url, err) |
| 513 | } |
| 514 | defer resp.Body.Close() |
| 515 | |
| 516 | if resp.StatusCode != http.StatusOK { |
| 517 | return "", fmt.Errorf("unexpected status code %d when downloading %s", resp.StatusCode, url) |
| 518 | } |
| 519 | |
| 520 | tempFile, err := os.CreateTemp("", "github-zip-*.zip") |
| 521 | if err != nil { |
| 522 | return "", fmt.Errorf("failed to create temp file: %w", err) |
| 523 | } |
| 524 | defer tempFile.Close() |
| 525 | |
| 526 | if _, err := io.Copy(tempFile, resp.Body); err != nil { |
| 527 | return "", fmt.Errorf("failed to save zip file: %w", err) |
| 528 | } |
| 529 | |
| 530 | return tempFile.Name(), nil |
| 531 | } |
| 532 | |
| 533 | // Function to copy a file from a source path to a destination path. |
| 534 | func copyFile(src, dst string) error { |
no test coverage detected