-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
120 lines (107 loc) · 3.42 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/Bhupesh-V/areyouok/links"
"github.com/Bhupesh-V/areyouok/report"
"github.com/Bhupesh-V/areyouok/utils"
)
var (
//Do not modify, its done at compile time using ldflags
aroVersion string = "dev" //aro Version
aroDate string = "dev" //aro Build Date
)
func driver(validLinks []map[string]string) ([]map[string]string, string) {
var wg sync.WaitGroup
var notoklinks []map[string]string
start := time.Now()
ch := make(chan map[string]string, len(validLinks)) //unbuffered channel
wg.Add(len(validLinks))
for _, v := range validLinks {
go links.CheckHealth(v["url"], &wg, ch)
}
go func() {
wg.Wait()
close(ch)
}()
for i := range validLinks {
notoklinks = append(notoklinks, <-ch)
fmt.Printf("\rAnalyzing %d/%d URLs", i+1, len(validLinks))
}
totalTime := fmt.Sprintf("%.2fs", time.Since(start).Seconds())
fmt.Printf("\nTotal Time: %.2fs\n", time.Since(start).Seconds())
return notoklinks, totalTime
}
func displayLinks(valid *map[string][]string) {
userDir := utils.GetUserDirectory()
// fmt.Printf("Found %s URL(s) across %s file(s)\n\n", strconv.Itoa(totalLinks), strconv.Itoa(totalFiles))
for file, urls := range *valid {
rel, _ := filepath.Rel(userDir, file)
fmt.Printf("%d 🔗️ %s\r\n", len(urls), rel)
}
fmt.Println()
}
func main() {
var (
typeOfFile string
ignoreDirs string
reportType string
dirs []string
)
flag.StringVar(&typeOfFile, "t", "md", "Specify `type` of files to scan")
flag.StringVar(&ignoreDirs, "i", "", "Comma separated directory and/or file names to `ignore`")
flag.StringVar(&reportType, "r", "", "Generate `report`. Supported formats include json, html, txt & github")
Version := flag.Bool("v", false, "Prints current AreYouOk `version`")
flag.Usage = func() {
fmt.Fprintf(os.Stdout, "AreYouOK URL Health Checker\n")
fmt.Fprintf(os.Stdout, "Usage: areyouok [OPTIONS] <directory-path>\nFollowing options are available:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stdout, "\nExample: areyouok -t=html -i=.git,README.md -r=json Documents/some-dir/\n\n")
fmt.Fprintf(os.Stdout, "Report Any Bugs via \nEmail : varshneybhupesh@gmail.com\nGitHub : https://github.com/Bhupesh-V/areyouok/issues/new/choose\n")
}
flag.Parse()
if *Version {
fmt.Printf("AreYouOk %s built on %s", aroVersion, aroDate)
os.Exit(0)
}
if ignoreDirs != "" {
dirs = strings.Split(ignoreDirs, ",")
}
if !utils.In(reportType, []string{"github", "json", "txt", "html", ""}) {
fmt.Printf("%s in not a supported report format\n", reportType)
os.Exit(1)
}
validFiles := links.GetFiles(typeOfFile, dirs)
links := links.GetLinks(validFiles)
displayLinks(&links.FileToListOfLinks)
data, totalTime := driver(links.AllHyperlinks)
healthData := make(map[string]interface{})
for _, v := range data {
urlMap := map[string]string{
"code": v["code"],
"message": v["message"],
"response_time": v["response_time"],
}
healthData[v["url"]] = urlMap
}
if reportType != "" {
rData := report.Report{
ReportType: &reportType,
ReportData: &report.ReportData{
TotalTime: &totalTime,
TotalLinks: links.TotalLinks,
TotalFiles: links.TotalValidFiles,
ValidFiles: links.FileToListOfLinks,
NotOkLinks: data,
CompleteHealthData: healthData,
},
}
rData.GenerateReport()
// generateReport(links, healthData, reportType)
}
}