-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (48 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"log"
"net/url"
"github.com/alecthomas/kingpin/v2"
"golang.org/x/sync/errgroup"
"github.com/previousnext/imaginator/internal/fileutils"
"github.com/previousnext/imaginator/internal/scrape"
)
var (
cliUser = kingpin.Flag("user", "Verbose mode.").String()
cliPass = kingpin.Flag("pass", "Verbose mode.").String()
cliSource = kingpin.Arg("source", "Verbose mode.").Required().String()
cliTarget = kingpin.Arg("target", "Verbose mode.").Required().String()
)
func main() {
kingpin.Parse()
err := run(*cliSource, *cliUser, *cliPass, *cliTarget)
if err != nil {
panic(err)
}
}
func run(source, user, pass, target string) error {
s, err := url.Parse(source)
if err != nil {
return err
}
images, err := scrape.Images(s, user, pass)
if err != nil {
return err
}
var g errgroup.Group
for _, image := range images {
image := image // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
target := fmt.Sprintf("%s%s", target, image.Path)
fmt.Println("Downloading:", target)
err := fileutils.Download(image.String(), user, pass, target)
if err != nil {
log.Println("Failed to download: %w\n", err)
}
return nil
})
}
// Wait for all HTTP fetches to complete.
return g.Wait()
}