linkchecker/main.go

64 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
"log"
)
func main() {
url := flag.String("url", "", "URL to check for broken links")
flag.Parse()
if *url == "" {
log.Fatal("Please provide a URL using the -url flag")
}
checker := NewLinkChecker()
brokenLinks, err := checker.CheckLinks(*url)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\nTotal pages checked: %d\n\n", checker.pagesChecked)
if len(brokenLinks) == 0 {
fmt.Println("No issues found!")
return
}
// First list redirects
var redirectCount int
for _, link := range brokenLinks {
if link.Redirect != nil {
if redirectCount == 0 {
fmt.Println("Redirects found:")
}
fmt.Printf("- %s (Redirect %d -> %s)\n", link.URL, link.StatusCode, link.Redirect.ToURL)
redirectCount++
}
}
if redirectCount > 0 {
fmt.Println()
}
// Then list broken links
var brokenCount int
for _, link := range brokenLinks {
if link.Redirect == nil {
if brokenCount == 0 {
fmt.Println("Broken links found:")
}
if link.Error != "" {
fmt.Printf("- %s (Error: %s)\n", link.URL, link.Error)
} else {
fmt.Printf("- %s (Status: %d)\n", link.URL, link.StatusCode)
}
brokenCount++
}
}
fmt.Printf("\nTotal issues: %d (%d redirects, %d broken)\n",
len(brokenLinks), redirectCount, brokenCount)
}