-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinkDetector.go
114 lines (89 loc) · 3.07 KB
/
linkDetector.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
package main
import (
"flag"
"fmt"
"os"
"sync"
"github.com/tonyvugithub/GoURLsCheckerCLI/lib/features"
"github.com/tonyvugithub/GoURLsCheckerCLI/lib/utils"
"github.com/tonyvugithub/GoURLsCheckerCLI/models"
)
var (
summary models.CheckSummary
wg sync.WaitGroup
userAgent *string
)
func main() {
//Create the channel for routine communication
channel := make(chan models.LinkStatus)
//version flag
versionFlag := flag.Bool("v", false, "version")
//Create sub-command
checkCmd := flag.NewFlagSet("check", flag.ExitOnError)
//flag.NewFlagSet("checkTelescope", flag.ExitOnError)
//Parse command-line args
flag.Parse()
//If there is only program name as argument, print help panel
if len(os.Args) < 2 {
utils.DisplayHelpPanel()
os.Exit(0)
}
//Display the version of the app if -v provided
features.CheckVersion(versionFlag)
//Switch statement to consider what subcommand provided
switch os.Args[1] {
//Check Subcommand
case "check":
flags := os.Args[2:]
dirFlag := checkCmd.Bool("d", false, "directory path input")
fileFlag := checkCmd.Bool("f", false, "file path input")
globFlag := checkCmd.Bool("g", false, "glob pattern")
reportFlag := checkCmd.Bool("r", false, "check report")
ignoreFlag := checkCmd.Bool("i", false, "ignore url list")
telescopeFlag := checkCmd.Bool("t", false, "check links from Telescope posts")
//Custom user-agent flag, using default user-agent for Go, access to http.defaultUserAgent deprecated
userAgent = checkCmd.String("u", "Go-http-client/1.1", "custom user-agent")
checkCmd.Parse(flags)
args := checkCmd.Args()
if *dirFlag && !*fileFlag {
//if directory flag was provided, check it by directory path
features.CheckWithDirectoryFlag(globFlag, args, channel, &wg, userAgent, &summary)
} else if *fileFlag && !*dirFlag {
//If file flag was provided, check it by Check by filepaths
features.CheckWithFileFlag(globFlag, args, channel, &wg, userAgent, &summary)
} else if *globFlag {
//If provided only -g flag then check the current directory
//Assign the glob pattern provided to a local variable
pattern := args[0]
features.CheckWithGlobFlag(pattern, []string{"."}, channel, &wg, userAgent, &summary)
} else if *ignoreFlag {
filePath := args[0]
fileData := utils.ReadFromFile(filePath)
ignoreList := utils.ParseIgnoreListPattern(fileData) // gets string with all links seprated by |
file := args[1]
features.CheckWithIgnoreFlag(ignoreList, file, channel, userAgent, &summary)
} else if *telescopeFlag {
features.CheckTelescopePosts(channel, userAgent, &summary)
} else {
fmt.Println("Invalid format!!! Please try again!!!")
}
wg.Wait()
//If there is a -r flag then report to file report.txt
if *reportFlag {
utils.WriteReportToFile(summary)
}
break
default:
fmt.Println("Expected 'check' command")
fmt.Println("Eg: $ linkDetector check ...")
break
}
//Exit with error code
numDownLinks := summary.GetNumDownLinks()
if numDownLinks > 0 {
fmt.Println("Exit with status code 1")
os.Exit(1)
}
fmt.Println("Exit with status code 0")
os.Exit(0)
}