-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
94 lines (83 loc) · 2.28 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
94
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
)
//initialize username argument and flag variables
func init() {
usr = getUsr()
if len(os.Args) >= 3 {
//work around for the username argument, start parsing flags from the third cmd line argument
//note that all flags are themselves cmd line arguments
flags.Parse(os.Args[2:])
}
//visit all flags that were set at the cmd line and append their names to SetFlags
flags.Visit(func(f *flag.Flag) {
SetFlags = append(SetFlags, f.Name)
if !searcher.search { //set search to true, since a search will be performed
searcher.search = true
}
})
}
func main() {
if usr == "" || *help {
printHelp()
os.Exit(3)
}
if '-' == usr[0] { //check if the username argument has an '-' character at its beginning
fmt.Print("Warning: username argument has a '-' character at its beginning")
os.Exit(3)
}
err := Fetch()
if err != nil {
log.Fatal(fmt.Errorf("error fetching data: %v", err))
}
results.Fprint(os.Stdout)
}
// Fetch fetches the data from GitHub and paginates,if neccessary.
func Fetch() error {
//Replace '@' with the given username
url := strings.Replace(URL, "@", usr, 1)
res, err := http.Get(url)
if err != nil {
return fmt.Errorf("check your internet connection")
}
if err = json.NewDecoder(res.Body).Decode(initResp); err == nil {
filter(initResp.Items, &results, searcher.search) //filter the initial resp.Items into results
if initResp.RepoCount() > 100 {
Paginate(url)
}
return nil
}
return fmt.Errorf("couldn't decode data properly")
}
//Paginate goes through each page up till the end
func Paginate(url string) {
numPages := (initResp.RepoCount() / 100) //compute number pages to request for
if initResp.RepoCount()%100 != 0 { //if the number of repos isn't a multiple of 100, one more page will be needed
numPages++
}
//initialize the tokens channel
tokens = make(chan struct{}, 19)
//spawn new goroutines to decode each page, starting from the second; Fetch() already got the first page
for i := 2; i <= numPages; i++ {
w.Add(1)
go decodePage(url, i)
}
//closer
go func() {
w.Wait()
close(errchan)
}()
//begin receiving errors from errchan, report any non-nil error
for err := range errchan {
if err != nil {
fmt.Printf("%v", err)
}
}
}