add the --download-ncbi option to obitaxonomy

This commit is contained in:
Eric Coissac
2025-01-29 12:38:39 +01:00
parent b6b18c0fa1
commit 8a28c9ae7c
5 changed files with 87 additions and 3 deletions

45
pkg/obiutils/download.go Normal file
View File

@ -0,0 +1,45 @@
package obiutils
import (
"fmt"
"io"
"net/http"
"os"
"github.com/schollz/progressbar/v3"
)
func DownloadFile(url string, filepath string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Create progress bar
bar := progressbar.DefaultBytes(
resp.ContentLength,
"downloading",
)
// Write the body to file while updating the progress bar
_, err = io.Copy(io.MultiWriter(out, bar), resp.Body)
if err != nil {
return err
}
return nil
}