feat: Add progress tracking and page checking information to link checker

This commit is contained in:
Erik Winter (aider) 2024-11-25 09:54:28 +01:00
parent 7020dbd47d
commit 667d102277
2 changed files with 13 additions and 6 deletions

View File

@ -10,14 +10,16 @@ import (
) )
type LinkChecker struct { type LinkChecker struct {
client *http.Client client *http.Client
visited map[string]bool visited map[string]bool
pagesChecked int
} }
func NewLinkChecker() *LinkChecker { func NewLinkChecker() *LinkChecker {
return &LinkChecker{ return &LinkChecker{
client: &http.Client{}, client: &http.Client{},
visited: make(map[string]bool), visited: make(map[string]bool),
pagesChecked: 0,
} }
} }
@ -50,7 +52,10 @@ func (lc *LinkChecker) checkLinksRecursive(pageURL string, brokenLinks []BrokenL
return brokenLinks, nil return brokenLinks, nil
} }
lc.visited[pageURL] = true lc.visited[pageURL] = true
lc.pagesChecked++
fmt.Printf("Checking page %d: %s\n", lc.pagesChecked, pageURL)
links, err := lc.getLinks(pageURL) links, err := lc.getLinks(pageURL)
if err != nil { if err != nil {
return nil, fmt.Errorf("error getting links: %w", err) return nil, fmt.Errorf("error getting links: %w", err)

View File

@ -20,12 +20,14 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
fmt.Printf("\nTotal pages checked: %d\n\n", checker.pagesChecked)
if len(brokenLinks) == 0 { if len(brokenLinks) == 0 {
fmt.Println("No broken links found!") fmt.Println("No broken links found!")
return return
} }
fmt.Println("Found broken links:") fmt.Printf("Found %d broken links:\n", len(brokenLinks))
for _, link := range brokenLinks { for _, link := range brokenLinks {
if link.Error != "" { if link.Error != "" {
fmt.Printf("- %s (Error: %s)\n", link.URL, link.Error) fmt.Printf("- %s (Error: %s)\n", link.URL, link.Error)