Skip to content

Commit e91861d

Browse files
committed
use godirwalk
1 parent f80ef25 commit e91861d

27 files changed

+1360
-26
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/dustin/go-humanize v1.0.0
1010
github.com/google/go-cmp v0.3.1 // indirect
1111
github.com/hashicorp/hcl/v2 v2.0.0
12+
github.com/karrick/godirwalk v1.13.0
1213
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
1314
github.com/spf13/cobra v0.0.5
1415
github.com/spf13/pflag v1.0.5 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
5454
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
5555
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
5656
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
57+
github.com/karrick/godirwalk v1.13.0 h1:GJq8GHQEAPsjwqfGhLNXBO5P0dS2HYdDRVWe+P4E/EQ=
58+
github.com/karrick/godirwalk v1.13.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
5759
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY=
5860
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
5961
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=

pkg/comments/comments.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111

1212
"github.com/augmentable-dev/lege"
13+
"github.com/karrick/godirwalk"
1314
"github.com/src-d/enry/v2"
1415
"gopkg.in/src-d/go-git.v4/plumbing/object"
1516
)
@@ -105,39 +106,41 @@ func SearchFile(filePath string, reader io.Reader) (Comments, error) {
105106
// SearchDir searches a directory for comments
106107
func SearchDir(dirPath string) (Comments, error) {
107108
found := make(Comments, 0)
108-
// TODO let's see what we can do concurrently here to speed up the processing
109-
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
110-
localPath, err := filepath.Rel(dirPath, path)
111-
if err != nil {
112-
return err
113-
}
114-
pathComponents := strings.Split(localPath, string(os.PathSeparator))
115-
// let's ignore git directories TODO: figure out a more generic way to set ignores
116-
matched, err := filepath.Match(".git", pathComponents[0])
117-
if err != nil {
118-
return err
119-
}
120-
if matched {
121-
return nil
122-
}
123-
if info.Mode().IsRegular() {
124-
p, err := filepath.Abs(path)
109+
err := godirwalk.Walk(dirPath, &godirwalk.Options{
110+
Callback: func(path string, de *godirwalk.Dirent) error {
111+
localPath, err := filepath.Rel(dirPath, path)
125112
if err != nil {
126113
return err
127114
}
128-
f, err := os.Open(p)
115+
pathComponents := strings.Split(localPath, string(os.PathSeparator))
116+
// let's ignore git directories TODO: figure out a more generic way to set ignores
117+
matched, err := filepath.Match(".git", pathComponents[0])
129118
if err != nil {
130119
return err
131120
}
132-
defer f.Close()
133-
t, err := SearchFile(p, f)
134-
if err != nil {
135-
return err
121+
if matched {
122+
return nil
136123
}
137-
c := Comments(t)
138-
found = append(found, c...)
139-
}
140-
return nil
124+
if de.IsRegular() {
125+
p, err := filepath.Abs(path)
126+
if err != nil {
127+
return err
128+
}
129+
f, err := os.Open(p)
130+
if err != nil {
131+
return err
132+
}
133+
defer f.Close()
134+
t, err := SearchFile(p, f)
135+
if err != nil {
136+
return err
137+
}
138+
c := Comments(t)
139+
found = append(found, c...)
140+
}
141+
return nil
142+
},
143+
Unsorted: true,
141144
})
142145
if err != nil {
143146
return nil, err

vendor/github.com/karrick/godirwalk/.gitignore

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/karrick/godirwalk/LICENSE

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/karrick/godirwalk/README.md

Lines changed: 223 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)