Browse Source

Use URL checker object

renovate/configure
Yota Toyama 8 years ago
parent
commit
e224e61be2
  1. 49
      main.go
  2. 8
      url_checker.go
  3. 20
      utils.go

49
main.go

@ -1,9 +1,7 @@
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
@ -11,7 +9,6 @@ import (
"github.com/a8m/mark"
"github.com/docopt/docopt-go"
"github.com/fatih/color"
"golang.org/x/net/html"
)
@ -37,41 +34,11 @@ func main() {
panic(err)
}
checkURLs(extractURLs(n), args["--verbose"].(bool))
}
func checkURLs(ss []string, verbose bool) {
client := &http.Client{Timeout: 5 * time.Second}
bs := make(chan bool, len(ss))
for _, s := range ss {
go checkURL(client, s, bs, verbose)
}
ok := true
for i := 0; i < len(ss); i++ {
ok = ok && <-bs
}
if !ok {
if !newURLChecker(5*time.Second, args["--verbose"].(bool)).CheckMany(extractURLs(n)) {
os.Exit(1)
}
}
func checkURL(client *http.Client, s string, bs chan bool, verbose bool) {
_, err := client.Get(s)
if s := color.New(color.FgCyan).SprintFunc()(s); err != nil {
printToStderr(
color.New(color.FgRed).SprintFunc()("ERROR") + "\t" + s + "\t" + err.Error())
} else if err == nil && verbose {
printToStderr(color.New(color.FgGreen).SprintFunc()("OK") + "\t" + s)
}
bs <- err == nil
}
func extractURLs(n *html.Node) []string {
ss := make(map[string]bool)
ns := make([]*html.Node, 0, 1024)
@ -99,16 +66,6 @@ func extractURLs(n *html.Node) []string {
return stringSetToSlice(ss)
}
func stringSetToSlice(s2b map[string]bool) []string {
ss := make([]string, 0, len(s2b))
for s := range s2b {
ss = append(ss, s)
}
return ss
}
func isURL(s string) bool {
u, err := url.Parse(s)
return err == nil && (u.Scheme == "http" || u.Scheme == "https")
@ -131,7 +88,3 @@ Options:
return args
}
func printToStderr(xs ...interface{}) {
fmt.Fprintln(os.Stderr, xs...)
}

8
url_checker.go

@ -29,13 +29,13 @@ func (c urlChecker) Check(s string) bool {
return err == nil
}
func (c urlChecker) CheckMany(ss []string) {
func (c urlChecker) CheckMany(ss []string) bool {
bs := make(chan bool, len(ss))
for _, s := range ss {
go func() {
bs <- c.Check()
}()
go func(s string) {
bs <- c.Check(s)
}(s)
}
ok := true

20
utils.go

@ -0,0 +1,20 @@
package main
import (
"fmt"
"os"
)
func stringSetToSlice(s2b map[string]bool) []string {
ss := make([]string, 0, len(s2b))
for s := range s2b {
ss = append(ss, s)
}
return ss
}
func printToStderr(xs ...interface{}) {
fmt.Fprintln(os.Stderr, xs...)
}
Loading…
Cancel
Save