-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (119 loc) · 3.15 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bufio"
"fmt"
"net/http"
"os"
"regexp"
"strings"
)
// Declare the constants that contain the strings to the colors
const (
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorReset = "\033[0m"
)
func main() {
filePath := getInput("Inform the filepath to the docs you want to check: ")
file, err := openFile(filePath)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
content, err := readFileContent(file)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
text := string(content)
fmt.Println("\nScanning for typos powered by gpt-4")
urlPatternHttps := `https?://[^\s()]+`
generalUrlsPattnern := `\[(.*?)\]\((.*?)\)`
buttonMatches := `href="([^"]+)"`
matchesHttp := findMatches(text, urlPatternHttps)
matches := findMatches(text, generalUrlsPattnern)
matchesButton := findMatches(text, buttonMatches)
for i := range matches {
x := strings.Split(matches[i], "](")
y := strings.Replace(x[1], ")", "", -1)
if (strings.Contains(y, "en/") || strings.Contains(y, "pt/") || strings.Contains(y, "pt-br/")) && !strings.Contains(y, "www.azion.com") {
y = formatURL(y)
}
matches[i] = y
}
for i := range matchesButton {
x := strings.Replace(matchesButton[i], `href="`, "", -1)
x = strings.ReplaceAll(x, "\"", "")
if (strings.Contains(x, "en/") || strings.Contains(x, "pt/") || strings.Contains(x, "pt-br/")) && !strings.Contains(x, "www.azion.com") {
x = formatURL(x)
}
matchesButton[i] = x
}
allURLs := append(matches, append(matchesHttp, matchesButton...)...)
fmt.Println("\nTesting links")
for _, link := range allURLs {
statusCode, err := checkURL(link)
if err != nil {
fmt.Println(colorRed, "Link:", link, "- Error:", err.Error(), colorReset)
continue
}
color := colorGreen
if statusCode > 400 {
color = colorRed
} else if statusCode >= 300 && statusCode < 400 {
color = colorYellow
}
fmt.Println(color, "Link:", link, "- Status Code:", statusCode, colorReset)
}
}
// Get the input informed by the user
func getInput(prompt string) string {
fmt.Print(prompt)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
return scanner.Text()
}
// Open the file
func openFile(filePath string) (*os.File, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
return file, nil
}
// Read the content inside the file (.mdx)
func readFileContent(file *os.File) ([]byte, error) {
stat, err := file.Stat()
if err != nil {
return nil, err
}
content := make([]byte, stat.Size())
_, err = file.Read(content)
if err != nil {
return nil, err
}
return content, nil
}
// Find mathches by the pattern (regex) informed
func findMatches(text, pattern string) []string {
re := regexp.MustCompile(pattern)
return re.FindAllString(text, -1)
}
// Format the URL adding the host
func formatURL(url string) string {
if !strings.HasSuffix(url, "/") {
url += "/"
}
return "https://www.azion.com" + url
}
// Check if the link is working
func checkURL(link string) (int, error) {
resp, err := http.Get(link)
if err != nil {
return 0, err
}
defer resp.Body.Close()
return resp.StatusCode, nil
}