linkchecker/main.go

39 lines
741 B
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 broken links found!")
return
}
fmt.Printf("Found %d broken links:\n", len(brokenLinks))
for _, link := range brokenLinks {
if link.Error != "" {
fmt.Printf("- %s (Error: %s)\n", link.URL, link.Error)
} else {
fmt.Printf("- %s (Status: %d)\n", link.URL, link.StatusCode)
}
}
}