Browse Source

Accept multiple files

renovate/configure
Yota Toyama 8 years ago
parent
commit
ef47d4e112
  1. 12
      examples/markdown.feature
  2. 35
      main.go

12
examples/markdown.feature

@ -68,3 +68,15 @@ Feature: Markdown
Then the exit status should be 1
And the stderr should contain "OK"
And the stderr should contain "ERROR"
Scenario: Check 2 markdown files
Given a file named "foo.md" with:
"""
[Google](https://google.com)
"""
And a file named "bar.md" with:
"""
[Yahoo](https://yahoo.com)
"""
When I successfully run `linkcheck foo.md bar.md`
Then the stdout should contain exactly ""

35
main.go

@ -21,22 +21,43 @@ func main() {
}()
args := getArgs()
fs := args["<filenames>"].([]string)
bs := make(chan bool, len(fs))
c := newURLChecker(5*time.Second, args["--verbose"].(bool))
for _, f := range fs {
go func(f string) {
bs <- checkFile(c, f)
}(f)
}
ok := true
for i := 0; i < len(fs); i++ {
ok = <-bs && ok
}
if !ok {
os.Exit(1)
}
}
bs, err := ioutil.ReadFile(args["<filename>"].(string))
func checkFile(c urlChecker, f string) bool {
bs, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
printToStderr(err.Error())
return false
}
n, err := html.Parse(strings.NewReader(mark.Render(string(bs))))
if err != nil {
panic(err)
printToStderr(err.Error())
return false
}
if !newURLChecker(5*time.Second, args["--verbose"].(bool)).CheckMany(extractURLs(n)) {
os.Exit(1)
}
return c.CheckMany(extractURLs(n))
}
func extractURLs(n *html.Node) []string {
@ -75,7 +96,7 @@ func getArgs() map[string]interface{} {
usage := `Link checker for Markdown and HTML
Usage:
linkcheck [-v] <filename>
linkcheck [-v] <filenames>...
Options:
-v, --verbose Be verbose`

Loading…
Cancel
Save