-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (74 loc) · 1.59 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bufio"
"context"
"log"
"os"
"strings"
"sync"
"time"
"github.com/go-git/go-git/v5"
)
var wg sync.WaitGroup
func readFile() []string {
filePath := os.Args[1]
readFile, err := os.Open(filePath)
if err != nil {
log.Println(err)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
var fileLines []string
for fileScanner.Scan() {
fileLines = append(fileLines, fileScanner.Text())
}
readFile.Close()
return fileLines
}
func getRepositoryName(repositoryURL string) string {
repositoryURLSplitted := strings.Split(repositoryURL, "/")
return repositoryURLSplitted[len(repositoryURLSplitted)-1]
}
func saveError(msg string) {
f, err := os.OpenFile("error.log",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
if _, err := f.WriteString(msg); err != nil {
log.Println(err)
}
}
func downloadRepository(ch chan string) string {
if len(ch) == 0 {
return "Finished"
} else {
defer wg.Done()
url := <-ch
ctx, _ := context.WithTimeout(context.Background(), 15*time.Minute)
log.Printf("Downloading %s ...", url)
_, err := git.PlainCloneContext(ctx, "/tmp/foo/"+getRepositoryName(url), false, &git.CloneOptions{
URL: url,
Progress: os.Stdout,
})
if err != nil {
log.Println("ERROR -", url)
saveError(url + "\n")
}
return downloadRepository(ch)
}
}
func main() {
urls := readFile()
max := len(urls)
ch := make(chan string, max)
for _, url := range urls {
ch <- url
}
wg.Add(max)
for i := 0; i < 4; i++ {
go downloadRepository(ch)
}
wg.Wait()
}