From b0b82cafb3a15b7215094d7b30415f83549813e1 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 18 Nov 2017 03:11:55 +0900 Subject: [PATCH] Create URL checker object --- url_checker.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 url_checker.go diff --git a/url_checker.go b/url_checker.go new file mode 100644 index 0000000..80f8b73 --- /dev/null +++ b/url_checker.go @@ -0,0 +1,48 @@ +package main + +import ( + "net/http" + "time" + + "github.com/fatih/color" +) + +type urlChecker struct { + client http.Client + verbose bool +} + +func newURLChecker(timeout time.Duration, verbose bool) urlChecker { + return urlChecker{http.Client{Timeout: timeout}, verbose} +} + +func (c urlChecker) Check(s string) bool { + _, err := c.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 && c.verbose { + printToStderr(color.New(color.FgGreen).SprintFunc()("OK") + "\t" + s) + } + + return err == nil +} + +func (c urlChecker) CheckMany(ss []string) { + bs := make(chan bool, len(ss)) + + for _, s := range ss { + go func() { + bs <- c.Check() + }() + } + + ok := true + + for i := 0; i < len(ss); i++ { + ok = ok && <-bs + } + + return ok +}