-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
93 lines (78 loc) · 1.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"golang.org/x/net/html"
"net/http"
"os"
"strings"
)
func main() {
if len(os.Args) == 1 {
fmt.Println("This program checks for broken links. Please specify -h for help.")
os.Exit(1)
}
parseArgs()
}
func parseArgs() {
if len(os.Args) < 3 {
printUsage()
}
option := os.Args[1]
switch option {
case "-h":
printUsage()
case "-u":
processLink(os.Args[2])
case "-f":
links := processFile(os.Args[2])
processLinks(links)
default:
printUsage()
}
}
func printUsage() {
fmt.Println("Usage: hdj -f [filename.html] or hdj -u [url]")
os.Exit(0)
}
func processLink(link string) {
fmt.Println(link)
os.Exit(0)
}
func processFile(filename string) []string {
bs, err := os.ReadFile(filename)
if err != nil {
fmt.Println("Error reading file: ", err)
os.Exit(1)
}
doc, err := html.Parse(strings.NewReader(string(bs)))
if err != nil {
fmt.Println("Error parsing HTML:", err)
os.Exit(1)
}
var links []string
var getLinks func(*html.Node)
getLinks = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "a" {
for _, attr := range n.Attr {
if attr.Key == "href" {
links = append(links, attr.Val)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
getLinks(c)
}
}
getLinks(doc)
return links
}
func processLinks(links []string) {
for _, l := range links {
resp, err := http.Get(l)
if err != nil {
fmt.Println(err.Error())
continue
}
fmt.Println(resp.Request.URL, "has a status of", resp.StatusCode)
}
}