Browse Source

Create file checker object

renovate/configure
Yota Toyama 8 years ago
parent
commit
d04e12a261
  1. 36
      file_checker.go
  2. 25
      main.go

36
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))
}

25
main.go

@ -1,15 +1,12 @@
package main package main
import ( import (
"bytes"
"io/ioutil"
"net/url" "net/url"
"os" "os"
"time" "time"
"github.com/docopt/docopt-go" "github.com/docopt/docopt-go"
"golang.org/x/net/html" "golang.org/x/net/html"
"gopkg.in/russross/blackfriday.v2"
) )
func main() { func main() {
@ -23,11 +20,11 @@ func main() {
args := getArgs() args := getArgs()
fs := args["<filenames>"].([]string) fs := args["<filenames>"].([]string)
bs := make(chan bool, len(fs)) 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 { for _, f := range fs {
go func(f string) { go func(f string) {
bs <- checkFile(c, f) bs <- c.Check(f)
}(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 { func extractURLs(n *html.Node) []string {
ss := make(map[string]bool) ss := make(map[string]bool)
ns := make([]*html.Node, 0, 1024) ns := make([]*html.Node, 0, 1024)

Loading…
Cancel
Save