-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
51 lines (45 loc) · 1.09 KB
/
utils.go
File metadata and controls
51 lines (45 loc) · 1.09 KB
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
package wgrpcd
import (
"fmt"
"net"
)
// IPNetsToStrings converts a list of net.IPNets to CIDR subnet strings.
func IPNetsToStrings(nets []net.IPNet) []string {
ips := []string{}
for _, net := range nets {
ips = append(ips, net.String())
}
return ips
}
// StringsToIPNet tries to convert a list of CIDR subnet strings to net.IPNets.
func StringsToIPNet(cidrStrings []string) ([]net.IPNet, error) {
ipNets := []net.IPNet{}
for _, cidr := range cidrStrings {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
ipNets = append(ipNets, *ipNet)
}
return ipNets, nil
}
// IPsToStrings converts a list of net.IPs to string
func IPsToStrings(ips []net.IP) []string {
rv := []string{}
for _, n := range ips {
rv = append(rv, n.String())
}
return rv
}
// StringsToIPs parses a list of strings into net.IPs.
func StringsToIPs(rawIPs []string) ([]net.IP, error) {
ips := []net.IP{}
for _, rawIP := range rawIPs {
ip := net.ParseIP(rawIP)
if ip == nil {
return []net.IP{}, fmt.Errorf("%v is not a valid IP address", ip)
}
ips = append(ips, ip)
}
return ips, nil
}