-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathverify_urls.go
67 lines (54 loc) · 1.63 KB
/
verify_urls.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
package main
import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"os"
"regexp"
"time"
)
func main() {
fmt.Println("Verifying URLs..")
readmeFile, err := os.ReadFile("README.md")
if err != nil {
fmt.Println("Could not find README!")
os.Exit(1)
}
fileContent := string(readmeFile)
urlElementRegex := regexp.MustCompile(`(?m)\[.+?]\(((http|https)://.+?)\)`)
httpClient := http.Client{
Timeout: 20 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{},
},
}
var brokenUrls []string
for _, urlElement := range urlElementRegex.FindAllStringSubmatch(fileContent, -1) {
var url = urlElement[1]
fmt.Printf("Checking %s: ", url)
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("User-Agent", "URL status code verification for the Flyeralarm onboarding resources; https://github.com/flyeralarm/onboarding")
resp, err := httpClient.Do(req)
errormessage := err
if errormessage == nil {
errormessage = errors.New(http.StatusText(resp.StatusCode))
resp.Body.Close()
}
if err != nil || resp.StatusCode != 200 {
brokenUrls = append(brokenUrls, url)
fmt.Println("FAILED - ", errormessage)
} else {
fmt.Println("OK")
}
}
if len(brokenUrls) != 0 {
fmt.Println("Broken URLs were found:")
for _, brokenUrl := range brokenUrls {
fmt.Println(brokenUrl)
}
os.Exit(1)
}
fmt.Println("No broken URLs found!")
os.Exit(0)
}