Skip to content

Commit 7abc5f5

Browse files
author
user
committed
Refactor isValidDomain to use regular expression
1 parent be32fe0 commit 7abc5f5

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

cmd/root.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,14 @@ import (
77
"net"
88
"os"
99
"strings"
10+
"regexp"
1011
)
1112

1213
func isValidDomain(url string) bool {
13-
url = strings.ToLower(url)
14-
//check if url is an ip address
15-
if ip := net.ParseIP(url); ip != nil {
16-
return true
17-
}
18-
19-
for _, c := range url {
20-
if c == '.' {
21-
continue
22-
}
23-
}
24-
subs := strings.Split(url, ".")
25-
if len(subs) < 2 {
26-
return false
27-
}
28-
return true
14+
//check if url is an ip address
15+
if ip := net.ParseIP(url); ip != nil { return true }
16+
if regexp.MustCompile(`^(([a-z-])\.?)*$`).MatchString(strings.ToLower(url)) { return true }
17+
return false
2918
}
3019

3120
var rootCmd = &cobra.Command{

0 commit comments

Comments
 (0)