null check for http.Head response

This commit is contained in:
raf 2023-12-24 20:42:18 +03:00
parent e5e16be391
commit 50298ea412
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29

29
main.go
View file

@ -62,23 +62,22 @@ func main() {
for _, match := range matches { for _, match := range matches {
link := strings.TrimSpace(match[2]) link := strings.TrimSpace(match[2])
resp, err := http.Head(link) resp, err := http.Head(link)
isInvalid := err != nil || resp.StatusCode == http.StatusNotFound || strings.Contains(err.Error(), "no such host") if err != nil {
if isInvalid {
logWithColor("ERROR", "Invalid link: %s", link) logWithColor("ERROR", "Invalid link: %s", link)
} else { continue
result := LinkCheckResult{ }
Link: link, result := LinkCheckResult{
IsValid: resp.StatusCode == http.StatusOK, Link: link,
StatusCode: resp.StatusCode, IsValid: resp.StatusCode == http.StatusOK,
} StatusCode: resp.StatusCode,
results = append(results, result) }
if *verbose || (!*failedOnly && !result.IsValid) { results = append(results, result)
statusColor := color.GreenString if *verbose || (!*failedOnly && !result.IsValid) {
if !result.IsValid { statusColor := color.GreenString
statusColor = color.RedString if !result.IsValid {
} statusColor = color.RedString
logWithColor("INFO", "%s: %s", link, statusColor("%d", result.StatusCode))
} }
logWithColor("INFO", "%s: %s", link, statusColor("%d", result.StatusCode))
} }
} }
} }