Skip to content

Commit

Permalink
make error channel empty by default
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Dec 11, 2017
1 parent e34cbeb commit 36f74d9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
13 changes: 9 additions & 4 deletions clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func newCloner(dest string, extract *regexp.Regexp) *cloner {
dest: dest,
extract: extract,
repos: make(chan string, maxBuffer),
err: make(chan error),
}

if c.git == "" {
Expand Down Expand Up @@ -71,7 +70,9 @@ func (cl *cloner) newWorker() {

if err != nil {
log.Println("Failed to clone", repo, err)
cl.err <- err
if cl.err != nil {
cl.err <- err
}
continue
}

Expand All @@ -91,7 +92,9 @@ func (cl *cloner) newWorker() {
return nil
}); err != nil {
log.Println("Failed to extract files", repo, extract.String(), err)
cl.err <- err
if cl.err != nil {
cl.err <- err
}
return
}
}
Expand All @@ -110,5 +113,7 @@ func (cl *cloner) start() {
func (cl *cloner) shutdown() {
close(cl.repos)
cl.wg.Wait()
close(cl.err)
if cl.err != nil {
close(cl.err)
}
}
4 changes: 3 additions & 1 deletion clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func testRepos(repos []string, t *testing.T) {
defer func() {
os.RemoveAll("test")
}()
c.err = make(chan error, 10)
c.start()

go func() {
Expand Down Expand Up @@ -90,6 +91,7 @@ func TestCloneWithExtract(t *testing.T) {
defer func() {
os.RemoveAll("test")
}()
c.err = make(chan error, 10)
c.start()

go func() {
Expand Down Expand Up @@ -117,7 +119,7 @@ func TestCloneWithExtract(t *testing.T) {
func TestCloneNotExistingRepo(t *testing.T) {
c := newCloner("test", nil)
c.ssh = true
c.err = make(chan error, 10) // Buffer errors to make test easier
c.err = make(chan error, 10)
c.start()

c.clone("rhysd/not-existing-repository")
Expand Down
8 changes: 1 addition & 7 deletions collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,8 @@ func (col *collector) searchRepos() (*github.RepositoriesSearchResult, error) {
func (col *collector) collect() (int, int, error) {
log.Println("Searching GitHub repositories with query:", col.query)
cloner := newCloner(col.dest, col.extract)
go func() {
for range cloner.err {
// Do nothing because error is already reported in cloner
// Error is reported in cloner because cloner can show more descriptive error message
}
}()

cloner.start()

total := 0
count := 0
for col.page <= col.maxPage {
Expand Down

0 comments on commit 36f74d9

Please sign in to comment.