-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.go
107 lines (89 loc) · 2.19 KB
/
utils.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
package utils
import (
"bufio"
"errors"
"github.com/ameenmaali/whoareyou/pkg/config"
"net/url"
"os"
"regexp"
"strings"
)
func GetUrlsFromFile(conf *config.Config) ([]string, error) {
deduplicatedUrls := make(map[string]bool)
var urls []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
providedUrl := scanner.Text()
// Only include properly formatted URLs
u, err := url.ParseRequestURI(providedUrl)
if err != nil {
if conf.DebugMode {
conf.Utils.PrintRed(os.Stderr, "url provided [%v] is not a properly formatted URL\n", providedUrl)
}
continue
}
if deduplicatedUrls[u.String()] {
continue
}
deduplicatedUrls[u.String()] = true
urls = append(urls, u.String())
}
return urls, scanner.Err()
}
func stringToRegex(value interface{}) (*regexp.Regexp, error) {
str, err := cleanString(value)
if err != nil {
return nil, err
}
re, err := regexp.Compile(str)
if err != nil {
return nil, err
}
return re, nil
}
func sliceToRegexSlice(value interface{}, matches []*regexp.Regexp) ([]*regexp.Regexp, error) {
values, ok := value.([]interface{})
if !ok {
return matches, errors.New("value provided is not a slice of strings")
}
for _, str := range values {
s, err := cleanString(str)
if err != nil {
continue
}
re, err := regexp.Compile(s)
if err != nil {
continue
}
matches = append(matches, re)
}
return matches, nil
}
func mapToRegexMap(value interface{}) (map[string]*regexp.Regexp, error) {
values, ok := value.(map[string]interface{})
if !ok {
return nil, errors.New("value provided is not a properly formated map")
}
regexMap := map[string]*regexp.Regexp{}
for key, val := range values {
re, err := regexp.Compile(val.(string))
if err != nil {
continue
}
regexMap[key] = re
}
return regexMap, nil
}
func cleanString(value interface{}) (string, error) {
str, ok := value.(string)
if !ok {
return "", errors.New("value provided is not a string")
}
splitStr := strings.Split(str, ";")
// Only take the first portion of the string, which contains the regex value
str = splitStr[0]
if endsWithSlash := strings.HasSuffix(str, "\\"); endsWithSlash {
str = strings.TrimSuffix(str, "\\")
}
return str, nil
}