11package utils
22
33import (
4+ "codacy/cli-v2/utils/logger"
45 "fmt"
56 "io"
6- "log"
77 "net/http"
88 "os"
99 "path/filepath"
10+
11+ "github.com/sirupsen/logrus"
1012)
1113
1214func DownloadFile (url string , destDir string ) (string , error ) {
13- log .Printf ("Attempting to download from URL: %s" , url )
15+ logger .Debug ("Attempting to download from URL" , logrus.Fields {
16+ "url" : url ,
17+ })
1418
1519 // Get the file name from the URL
1620 fileName := filepath .Base (url )
17- log .Printf ("Target filename: %s" , fileName )
21+ logger .Debug ("Target filename determined" , logrus.Fields {
22+ "fileName" : fileName ,
23+ })
1824
1925 // Create the destination file path
2026 destPath := filepath .Join (destDir , fileName )
21- log .Printf ("Destination path: %s" , destPath )
27+ logger .Debug ("Destination path set" , logrus.Fields {
28+ "destPath" : destPath ,
29+ })
2230
2331 _ , errInfo := os .Stat (destPath )
2432 if errInfo != nil && os .IsExist (errInfo ) {
25- log .Printf ("File already exists at destination, skipping download" )
33+ logger .Debug ("File already exists at destination, skipping download" , logrus.Fields {
34+ "destPath" : destPath ,
35+ })
2636 return destPath , nil
2737 }
2838
@@ -34,7 +44,9 @@ func DownloadFile(url string, destDir string) (string, error) {
3444 defer outFile .Close ()
3545
3646 // Make the HTTP GET request
37- log .Printf ("Making HTTP GET request..." )
47+ logger .Debug ("Making HTTP GET request" , logrus.Fields {
48+ "url" : url ,
49+ })
3850 client := & http.Client {}
3951 req , err := http .NewRequest ("GET" , url , nil )
4052 if err != nil {
@@ -54,12 +66,14 @@ func DownloadFile(url string, destDir string) (string, error) {
5466 }
5567
5668 // Copy the response body to the destination file
57- log . Printf ("Downloading file content... " )
69+ logger . Debug ("Downloading file content" )
5870 written , err := io .Copy (outFile , resp .Body )
5971 if err != nil {
6072 return "" , fmt .Errorf ("failed to copy file contents: %w" , err )
6173 }
62- log .Printf ("Downloaded %d bytes" , written )
74+ logger .Debug ("Downloaded file successfully" , logrus.Fields {
75+ "bytes" : written ,
76+ })
6377
6478 if written == 0 {
6579 return "" , fmt .Errorf ("downloaded file is empty (0 bytes)" )
0 commit comments