-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcidr.go
68 lines (56 loc) · 1.68 KB
/
cidr.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
package clcv2
/*
* Routines to specify/parse CIDR strings
*/
import (
"fmt"
"net"
"strings"
"github.com/pkg/errors"
)
// CIDRs implements the flag.Value interface, allowing to speciy multiple CIDR values.
type CIDRs []string
// String implements the flag.Value String method for CIDRs.
func (c CIDRs) String() string {
var cidrs = make([]string, len(c))
for i, cidr := range c {
cidrs[i] = fmt.Sprint(cidr)
}
return fmt.Sprintf("[%s]", strings.Join(cidrs, ", "))
}
// Set implements the flag.Value Set method for CIDRs.
func (c *CIDRs) Set(val string) error {
_, net, err := net.ParseCIDR(val)
if err != nil {
return errors.Errorf("invalid CIDR format %q: %s", val, err)
}
*c = append(*c, net.String())
return nil
}
// SrcRestrictions is analogous to CIDRs. A separate implementation is needed, since it has a different type.
type SrcRestrictions []SourceCIDR
// SourceCIDR wraps the IP range allowed to access a public IP, specified using CIDR notation.
type SourceCIDR struct {
Cidr string `json:"cidr"`
}
// String implements the flag.Value String method for SrcRestrictions.
func (s SrcRestrictions) String() string {
var cidrs = make([]string, len(s))
for i, cidr := range s {
cidrs[i] = cidr.Cidr
}
return fmt.Sprintf("[%s]", strings.Join(cidrs, ", "))
}
// Type implements pflag.Value.Type
func (*SrcRestrictions) Type() string {
return "CLCv2 CIDR Source Restrictions"
}
// Set implements the flag.Value Set method for SrcRestrictions.
func (s *SrcRestrictions) Set(val string) error {
_, net, err := net.ParseCIDR(val)
if err != nil {
return errors.Errorf("invalid source restriction format %q: %s", val, err)
}
*s = append(*s, SourceCIDR{net.String()})
return nil
}