downloadFile downloads a file from a URL and saves it to a destination path
(url, dest string)
| 78 | |
| 79 | // downloadFile downloads a file from a URL and saves it to a destination path |
| 80 | func downloadFile(url, dest string) error { |
| 81 | resp, err := http.Get(url) |
| 82 | if err != nil { |
| 83 | return fmt.Errorf("failed to perform HTTP GET request: %w", err) |
| 84 | } |
| 85 | defer resp.Body.Close() |
| 86 | |
| 87 | if resp.StatusCode != http.StatusOK { |
| 88 | return fmt.Errorf("unexpected HTTP status: %s", resp.Status) |
| 89 | } |
| 90 | |
| 91 | out, err := os.Create(dest) |
| 92 | if err != nil { |
| 93 | return fmt.Errorf("failed to create file: %w", err) |
| 94 | } |
| 95 | defer out.Close() |
| 96 | |
| 97 | _, err = io.Copy(out, resp.Body) |
| 98 | if err != nil { |
| 99 | return fmt.Errorf("failed to copy response body to file: %w", err) |
| 100 | } |
| 101 | |
| 102 | return nil |
| 103 | } |