-
Notifications
You must be signed in to change notification settings - Fork 1
/
repository.go
97 lines (86 loc) · 1.99 KB
/
repository.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
95
96
97
package main
import (
"sync"
"github.com/google/go-github/github"
"context"
)
type Repo struct {
search *Search
maxItem int
printCount int
}
func NewRepo(s *Search) *Repo {
return &Repo{s, 0, 0}
}
func (r *Repo) Search(c context.Context) (<-chan []github.Repository, <-chan error) {
var wg sync.WaitGroup
reposBuff := make(chan []github.Repository, 1)
errChan := make(chan error, 1)
// 1st search
repos, lastPage, max, err := r.search.First(c)
if err != nil {
Debug("Error First()\n")
errChan <- err
return reposBuff, errChan
}
r.maxItem = max
// notify main thread of first search result
reposBuff <- repos
// 2nd - 10th search
go func() {
for page := 2; page < lastPage+1; page++ {
Debug("sub thread start %d\n", page)
wg.Add(1)
go func(p int) {
// notify main thread of 2nd - 10th search result
rs, err := r.search.Exec(c,p)
if err != nil {
Debug("sub thread error %d\n", p)
errChan <- err
}
reposBuff <- rs
wg.Done()
Debug("sub thread end %d\n", p)
}(page)
}
Debug("sub thread wait...\n")
wg.Wait()
Debug("sub thread wakeup!!\n")
close(reposBuff)
}()
Debug("main thread return\n")
return reposBuff, errChan
}
func (r *Repo) Print(repos []github.Repository) (bool, int) {
Debug("repos length %d\n", len(repos))
repoNameMaxLen := 0
for _, repo := range repos {
repoNamelen := len(*repo.FullName)
if repoNamelen > repoNameMaxLen {
repoNameMaxLen = repoNamelen
}
}
printLine := func(repo *github.Repository) {
if repo.FullName != nil {
Printf("%v", *repo.FullName)
}
Printf(" ")
paddingLen := repoNameMaxLen - len(*repo.FullName)
for i := 0; i < paddingLen; i++ {
Printf(" ")
}
if repo.Description != nil {
Printf("%v", *repo.Description)
}
Printf("\n")
}
for _, repo := range repos {
printLine(&repo)
r.printCount++
Debug("printCount %d, r.maxItem %d\n", r.printCount, r.maxItem)
if r.printCount >= r.maxItem {
return true, r.printCount
}
}
return false, r.printCount
}