diff --git a/arguments.go b/arguments.go index 7fde176..509fe03 100644 --- a/arguments.go +++ b/arguments.go @@ -1,17 +1,24 @@ package main -import "github.com/docopt/docopt-go" +import ( + "strconv" + "time" + + "github.com/docopt/docopt-go" +) const usage = `Link checker for Markdown and HTML Usage: - linkcheck [-v] ... + linkcheck [-t ] [-v] ... Options: - -v, --verbose Be verbose` + -v, --verbose Be verbose + -t, --timeout Set timeout for HTTP requests in seconds [default: 5]` type arguments struct { filenames []string + timeout time.Duration verbose bool } @@ -22,5 +29,15 @@ func getArgs() (arguments, error) { return arguments{}, err } - return arguments{args[""].([]string), args["--verbose"].(bool)}, nil + f, err := strconv.ParseFloat(args["--timeout"].(string), 64) + + if err != nil { + return arguments{}, err + } + + return arguments{ + args[""].([]string), + time.Duration(f) * time.Second, + args["--verbose"].(bool), + }, nil } diff --git a/examples/markdown.feature b/examples/markdown.feature index d600a4a..ab7f9b0 100644 --- a/examples/markdown.feature +++ b/examples/markdown.feature @@ -104,3 +104,11 @@ Feature: Markdown """ When I successfully run `sh foo.sh` Then the stdout should contain exactly "5" + + Scenario: Check a markdown file which contains a live link with timeout + Given a file named "foo.md" with: + """ + [Google](https://google.com) + """ + When I successfully run `linkcheck --timeout 10 foo.md` + Then the stdout should contain exactly "" diff --git a/main.go b/main.go index 0d02371..74f2abd 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,6 @@ package main -import ( - "os" - "time" -) +import "os" func main() { args, err := getArgs() @@ -15,7 +12,7 @@ func main() { fs := args.filenames rc := make(chan fileResult, len(fs)) - c := newFileChecker(5 * time.Second) + c := newFileChecker(args.timeout) go c.CheckMany(fs, rc)