interepet invalid host errors as invalid links

This commit is contained in:
raf 2023-12-24 21:58:26 +03:00
parent 768fc24f2b
commit 4ba90a1367
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29

56
main.go
View file

@ -20,7 +20,7 @@ type LinkCheckResult struct {
} }
func logWithColor(level string, msg string, args ...interface{}) { func logWithColor(level string, msg string, args ...interface{}) {
timestamp := time.Now().Format("2006/01/02 15:04:05") // TODO: use time.RFC3339? timestamp := time.Now().Format("2006/01/02 15:04:05")
colorFunc := color.New(color.FgWhite).SprintFunc() colorFunc := color.New(color.FgWhite).SprintFunc()
switch level { switch level {
case "ERROR": case "ERROR":
@ -49,9 +49,17 @@ func main() {
logWithColor("ERROR", "Failed to open file: %v", err) logWithColor("ERROR", "Failed to open file: %v", err)
os.Exit(1) os.Exit(1)
} }
if file == nil {
logWithColor("ERROR", "File is nil")
os.Exit(1)
}
defer file.Close() defer file.Close()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
if scanner == nil {
logWithColor("ERROR", "Scanner is nil")
os.Exit(1)
}
re := regexp.MustCompile(`\[(.*?)\]\((.*?)\)`) re := regexp.MustCompile(`\[(.*?)\]\((.*?)\)`)
results := []LinkCheckResult{} results := []LinkCheckResult{}
@ -62,22 +70,28 @@ 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)
if err != nil { if err != nil || resp == nil {
logWithColor("ERROR", "Invalid link: %s", link) logWithColor("ERROR", "Invalid link: %s", link)
continue results = append(results, LinkCheckResult{
} Link: link,
result := LinkCheckResult{ IsValid: false,
Link: link, StatusCode: http.StatusBadRequest,
IsValid: resp.StatusCode == http.StatusOK, })
StatusCode: resp.StatusCode, } else {
} isValid := resp.StatusCode == http.StatusOK
results = append(results, result) result := LinkCheckResult{
if *verbose || (!*failedOnly && !result.IsValid) { Link: link,
statusColor := color.GreenString IsValid: isValid,
if !result.IsValid { StatusCode: resp.StatusCode,
statusColor = color.RedString }
results = append(results, result)
if *verbose || (!*failedOnly && !isValid) {
statusColor := color.GreenString
if !isValid {
statusColor = color.RedString
}
logWithColor("INFO", "%s: %s", link, statusColor("%d", resp.StatusCode))
} }
logWithColor("INFO", "%s: %s", link, statusColor("%d", result.StatusCode))
} }
} }
} }
@ -97,18 +111,18 @@ func main() {
invalidCount++ invalidCount++
} }
} }
summaryColor := color.GreenString
if invalidCount > 0 {
summaryColor = color.RedString
}
logWithColor("INFO", summaryColor("Summary: %d valid links, %d invalid links"), validCount, invalidCount)
if *failedOnly { if *failedOnly {
for _, result := range results { for _, result := range results {
if !result.IsValid { if !result.IsValid {
logWithColor("ERROR", "Failed link: %s", result.Link) logWithColor("ERROR", "Failed link: %s", result.Link)
} }
} }
} else {
summaryColor := color.GreenString
if invalidCount > 0 {
summaryColor = color.RedString
}
logWithColor("INFO", summaryColor("Summary: %d valid links, %d invalid links"), validCount, invalidCount)
} }
if invalidCount > 0 { if invalidCount > 0 {