downloader/main.go

52 lines
755 B
Go
Raw Normal View History

2023-01-20 18:42:35 +00:00
package main
import (
"io"
"log"
"net/http"
"os"
"strings"
)
func main() {
entries := os.Args[1:]
c := make(chan error)
d := Downloader{}
for _, u := range entries {
splits := strings.Split(u, ";")
url := splits[0]
output := splits[1]
go d.download(url, output, c)
}
for range entries {
err := <-c
if err != nil {
log.Panic(err)
}
}
}
type Downloader struct {
HTTPClient http.Client
}
func (d *Downloader) download(url string, output string, doneChan chan error) {
res, err := d.HTTPClient.Get(url)
if err != nil {
doneChan <- err
return
}
f, err := os.Create(output)
if err != nil {
doneChan <- err
return
}
_, err = io.Copy(f, res.Body)
if err != nil {
doneChan <- err
return
}
doneChan <- nil
}