-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
87 lines (73 loc) · 1.8 KB
/
helper.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
package kmip
import (
"fmt"
"net"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
)
func ContainsEnum(slice []Enum, item Enum) bool {
for _, element := range slice {
if element == item {
return true
}
}
return false
}
func ContainsString(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func ValidateDuration(input string) error {
// Regex pattern for duration strings like "1y", "6M", "30d", "12h", "45m", "30s"
durationRegex := regexp.MustCompile(`^(\d+)([yMwdhms])$`)
matches := durationRegex.FindStringSubmatch(input)
if len(matches) != 3 {
return errors.New("invalid format. Expected format: 1y, 6M, 30d, etc.")
}
// Convert the number part
value, err := strconv.Atoi(matches[1])
if err != nil || value <= 0 {
return errors.New("duration must be a positive number")
}
// Check the unit
unit := matches[2]
switch unit {
case "y", "M", "w", "d", "h", "m", "s":
return nil
default:
return errors.New("unsupported duration unit")
}
}
func IsValidHostname(hostname string) bool {
if hostname == "localhost" {
return true
}
hostnameRegex := `^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`
re := regexp.MustCompile(hostnameRegex)
return re.MatchString(hostname)
}
func IsValidIP(ip string) bool {
return net.ParseIP(ip) != nil
}
func ValidateHostnamesOrIPs(input string) error {
if input == "" {
return errors.New("missing value for field")
}
entries := strings.Split(input, ",")
for _, entry := range entries {
trimmedEntry := strings.TrimSpace(entry)
if trimmedEntry == "" {
return errors.New("empty value found in the list")
}
if !IsValidHostname(trimmedEntry) && !IsValidIP(trimmedEntry) {
return fmt.Errorf("invalid hostname or IP: %s", trimmedEntry)
}
}
return nil
}