generalize cidr parsing and improve lua tests
This commit is contained in:
parent
2254a91866
commit
2cff9fa41d
7 changed files with 68 additions and 70 deletions
|
|
@ -18,6 +18,7 @@ package net
|
|||
|
||||
import (
|
||||
"net"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -51,3 +52,30 @@ func ParseIPNets(specs ...string) (IPNet, IP, error) {
|
|||
|
||||
return ipnetset, ipset, nil
|
||||
}
|
||||
|
||||
// ParseCIDRs parses comma separated CIDRs into a sorted string array
|
||||
func ParseCIDRs(s string) ([]string, error) {
|
||||
if s == "" {
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
values := strings.Split(s, ",")
|
||||
|
||||
ipnets, ips, err := ParseIPNets(values...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cidrs := []string{}
|
||||
for k := range ipnets {
|
||||
cidrs = append(cidrs, k)
|
||||
}
|
||||
|
||||
for k := range ips {
|
||||
cidrs = append(cidrs, k)
|
||||
}
|
||||
|
||||
sort.Strings(cidrs)
|
||||
|
||||
return cidrs, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue