Browse Source

Use channel to feed filenames

renovate/configure
Yota Toyama 8 years ago
parent
commit
c2412e4aaa
  1. 4
      file_checker.go
  2. 39
      main.go
  3. 41
      utils.go
  4. 10
      utils_test.go

4
file_checker.go

@ -39,10 +39,10 @@ func (c fileChecker) Check(f string) ([]urlResult, error) {
return rs, nil return rs, nil
} }
func (c fileChecker) CheckMany(fs []string, rc chan<- fileResult) { func (c fileChecker) CheckMany(fc <-chan string, rc chan<- fileResult) {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
for _, f := range fs { for f := range fc {
wg.Add(1) wg.Add(1)
go func(f string) { go func(f string) {

39
main.go

@ -10,20 +10,15 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if args.recursive { fc := make(chan string, 1024)
args.filenames, err = listFilesRecursively(args.filenames)
if err != nil { go findMarkupFiles(args.filenames, args.recursive, fc)
printToStderr(err.Error())
os.Exit(1)
}
}
rc := make(chan fileResult, len(args.filenames)) rc := make(chan fileResult, 1024)
s := newSemaphore(args.concurrency) s := newSemaphore(args.concurrency)
c := newFileChecker(args.timeout, s) c := newFileChecker(args.timeout, s)
go c.CheckMany(args.filenames, rc) go c.CheckMany(fc, rc)
ok := true ok := true
@ -40,29 +35,3 @@ func main() {
os.Exit(1) os.Exit(1)
} }
} }
func listFilesRecursively(fs []string) ([]string, error) {
gs := []string{}
for _, f := range fs {
i, err := os.Stat(f)
if err != nil {
return nil, err
}
if i.IsDir() {
fs, err := listFiles(f)
if err != nil {
return nil, err
}
gs = append(gs, fs...)
} else {
gs = append(gs, f)
}
}
return gs, nil
}

41
utils.go

@ -5,7 +5,9 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"github.com/fatih/color"
"github.com/kr/text" "github.com/kr/text"
) )
@ -23,14 +25,18 @@ func printToStderr(xs ...interface{}) {
fmt.Fprintln(os.Stderr, xs...) fmt.Fprintln(os.Stderr, xs...)
} }
func fail(err error) {
s := err.Error()
printToStderr(color.RedString(strings.ToUpper(s[:1]) + s[1:]))
os.Exit(1)
}
func indent(s string) string { func indent(s string) string {
return text.Indent(s, "\t") return text.Indent(s, "\t")
} }
func listFiles(d string) ([]string, error) { func listDirectory(d string, fc chan<- string) error {
fs := []string{} return filepath.Walk(d, func(f string, i os.FileInfo, err error) error {
err := filepath.Walk(d, func(f string, i os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
@ -42,15 +48,34 @@ func listFiles(d string) ([]string, error) {
} }
if !i.IsDir() && !b && isMarkupFile(f) { if !i.IsDir() && !b && isMarkupFile(f) {
fs = append(fs, f) fc <- f
} }
return nil return nil
}) })
}
func findMarkupFiles(fs []string, recursive bool, fc chan<- string) {
for _, f := range fs {
i, err := os.Stat(f)
if err != nil { if err != nil {
return nil, err fail(err)
}
if i.IsDir() && recursive {
err := listDirectory(f, fc)
if err != nil {
fail(err)
}
} else if i.IsDir() {
fail(fmt.Errorf("%v is not a file", f))
} else {
fc <- f
}
} }
return fs, nil close(fc)
} }

10
utils_test.go

@ -7,13 +7,15 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestListFiles(t *testing.T) { func TestListDirectory(t *testing.T) {
fs, err := listFiles(".") fc := make(chan string, 1024)
err := listDirectory(".", fc)
close(fc)
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
assert.NotEqual(t, 0, len(fs)) assert.NotEqual(t, 0, len(fc))
for _, f := range fs { for f := range fc {
i, err := os.Stat(f) i, err := os.Stat(f)
assert.True(t, isMarkupFile(f)) assert.True(t, isMarkupFile(f))

Loading…
Cancel
Save