Add error handling to file downloads
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ask Bjørn Hansen 2023-09-06 00:14:37 -07:00
parent e6a44bc92e
commit ce01df8b4a
3 changed files with 51 additions and 2 deletions

View File

@ -209,7 +209,7 @@ func validateData(dataDir string) {
} }
if downloadFiles { if downloadFiles {
fmt.Println("Downloading CSV files from OurAirports.com...") fmt.Println("Downloading CSV files from OurAirports.com...")
alphafoxtrot.DownloadDatabase(dataDir) DownloadDatabase(dataDir)
} }
} }

49
downloader.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"errors"
"fmt"
"path"
"time"
alphafoxtrot "github.com/grumpypixel/go-airport-finder"
"github.com/grumpypixel/go-webget"
)
// Download csv files from OurAirports.com
func DownloadDatabase(targetDir string) error {
files := make([]string, 0)
for _, filename := range alphafoxtrot.OurAirportsFiles {
files = append(files, alphafoxtrot.OurAirportsBaseURL+filename)
}
var errs []error
for _, url := range files {
options := webget.Options{
ProgressHandler: MyProgress{},
Timeout: time.Second * 60,
CreateTargetDir: true,
}
err := webget.DownloadToFile(url, targetDir, "", &options)
if err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
type MyProgress struct{}
func (p MyProgress) Start(sourceURL string) {
// fmt.Println()
}
func (p MyProgress) Update(sourceURL string, percentage float64, bytesRead, contentLength int64) {
if percentage > 0 {
fmt.Printf("\rDownloading %s: %v bytes [%.2f%%]", path.Base(sourceURL), bytesRead, percentage)
}
fmt.Printf("\rDownloading %s: %v bytes [done]", path.Base(sourceURL), bytesRead)
}
func (p MyProgress) Done(sourceURL string) {
fmt.Println()
}

2
go.mod
View File

@ -5,12 +5,12 @@ go 1.21
require ( require (
github.com/golang/geo v0.0.0-20230421003525-6adc56603217 github.com/golang/geo v0.0.0-20230421003525-6adc56603217
github.com/grumpypixel/go-airport-finder v0.0.0-20210902211810-793a4fb1490b github.com/grumpypixel/go-airport-finder v0.0.0-20210902211810-793a4fb1490b
github.com/grumpypixel/go-webget v0.0.1
github.com/labstack/echo/v4 v4.11.1 github.com/labstack/echo/v4 v4.11.1
) )
require ( require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/grumpypixel/go-webget v0.0.1 // indirect
github.com/labstack/gommon v0.4.0 // indirect github.com/labstack/gommon v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-isatty v0.0.19 // indirect