asdf
This commit is contained in:
commit
61142dd767
2 changed files with 54 additions and 0 deletions
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module gitea.nulo.in/Nulo/downloader
|
||||
|
||||
go 1.19
|
51
main.go
Normal file
51
main.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
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
|
||||
}
|
Loading…
Reference in a new issue