diff --git a/file_checker.go b/file_checker.go new file mode 100644 index 0000000..8b41f1e --- /dev/null +++ b/file_checker.go @@ -0,0 +1,36 @@ +package main + +import ( + "bytes" + "io/ioutil" + "time" + + "golang.org/x/net/html" + "gopkg.in/russross/blackfriday.v2" +) + +type fileChecker struct { + urlChecker urlChecker +} + +func newFileChecker(timeout time.Duration, verbose bool) fileChecker { + return fileChecker{newURLChecker(timeout, verbose)} +} + +func (c fileChecker) Check(f string) bool { + bs, err := ioutil.ReadFile(f) + + if err != nil { + printToStderr(err.Error()) + return false + } + + n, err := html.Parse(bytes.NewReader(blackfriday.Run(bs))) + + if err != nil { + printToStderr(err.Error()) + return false + } + + return c.urlChecker.CheckMany(extractURLs(n)) +} diff --git a/main.go b/main.go index ba9e19d..d1ef5b7 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,12 @@ package main import ( - "bytes" - "io/ioutil" "net/url" "os" "time" "github.com/docopt/docopt-go" "golang.org/x/net/html" - "gopkg.in/russross/blackfriday.v2" ) func main() { @@ -23,11 +20,11 @@ func main() { args := getArgs() fs := args[""].([]string) bs := make(chan bool, len(fs)) - c := newURLChecker(5*time.Second, args["--verbose"].(bool)) + c := newFileChecker(5*time.Second, args["--verbose"].(bool)) for _, f := range fs { go func(f string) { - bs <- checkFile(c, f) + bs <- c.Check(f) }(f) } @@ -42,24 +39,6 @@ func main() { } } -func checkFile(c urlChecker, f string) bool { - bs, err := ioutil.ReadFile(f) - - if err != nil { - printToStderr(err.Error()) - return false - } - - n, err := html.Parse(bytes.NewReader(blackfriday.Run(bs))) - - if err != nil { - printToStderr(err.Error()) - return false - } - - return c.CheckMany(extractURLs(n)) -} - func extractURLs(n *html.Node) []string { ss := make(map[string]bool) ns := make([]*html.Node, 0, 1024)