Browse Source

Use markup file finder

renovate/configure
Yota Toyama 8 years ago
parent
commit
7209472361
  1. 9
      main.go
  2. 48
      utils.go
  3. 24
      utils_test.go

9
main.go

@ -12,15 +12,14 @@ func main() {
fail(err) fail(err)
} }
fc := make(chan string, maxOpenFiles) m := newMarkupFileFinder()
ec := make(chan error, 64)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
go findMarkupFiles(args.filenames, args.recursive, fc, ec) go m.Find(args.filenames, args.recursive)
wg.Add(1) wg.Add(1)
go func() { go func() {
for e := range ec { for e := range m.Errors() {
fail(e) fail(e)
} }
@ -31,7 +30,7 @@ func main() {
s := newSemaphore(args.concurrency) s := newSemaphore(args.concurrency)
c := newFileChecker(args.timeout, args.documentRoot, s) c := newFileChecker(args.timeout, args.documentRoot, s)
go c.CheckMany(fc, rc) go c.CheckMany(m.Filenames(), rc)
ok := true ok := true

48
utils.go

@ -3,8 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"regexp"
"strings" "strings"
"github.com/fatih/color" "github.com/fatih/color"
@ -34,49 +32,3 @@ func fail(err error) {
func indent(s string) string { func indent(s string) string {
return text.Indent(s, "\t") return text.Indent(s, "\t")
} }
func listDirectory(d string, fc chan<- string) error {
return filepath.Walk(d, func(f string, i os.FileInfo, err error) error {
if err != nil {
return err
}
b, err := regexp.MatchString("(^\\.)|(/\\.)", f)
if err != nil {
return err
}
if !i.IsDir() && !b && isMarkupFile(f) {
fc <- f
}
return nil
})
}
func findMarkupFiles(fs []string, recursive bool, fc chan<- string, ec chan<- error) {
for _, f := range fs {
i, err := os.Stat(f)
if err != nil {
ec <- err
continue
}
if i.IsDir() && recursive {
err := listDirectory(f, fc)
if err != nil {
ec <- err
}
} else if i.IsDir() {
ec <- fmt.Errorf("%v is not a file", f)
} else {
fc <- f
}
}
close(fc)
close(ec)
}

24
utils_test.go

@ -1,25 +1 @@
package main package main
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListDirectory(t *testing.T) {
fc := make(chan string, 1024)
err := listDirectory(".", fc)
close(fc)
assert.Equal(t, nil, err)
assert.NotEqual(t, 0, len(fc))
for f := range fc {
i, err := os.Stat(f)
assert.True(t, isMarkupFile(f))
assert.Equal(t, nil, err)
assert.False(t, i.IsDir())
}
}

Loading…
Cancel
Save