forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipnet_slice.go
155 lines (131 loc) · 4.19 KB
/
ipnet_slice.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag
import (
"fmt"
"net"
"strings"
)
// -- ipNetSlice Value
type ipNetSliceValue struct {
value *[]net.IPNet
changed bool
}
var _ Value = (*ipNetSliceValue)(nil)
var _ Getter = (*ipNetSliceValue)(nil)
var _ SliceValue = (*ipNetSliceValue)(nil)
var _ Typed = (*ipNetSliceValue)(nil)
func newIPNetSliceValue(val []net.IPNet, p *[]net.IPNet) *ipNetSliceValue {
ipnsv := new(ipNetSliceValue)
ipnsv.value = p
*ipnsv.value = val
return ipnsv
}
func (s *ipNetSliceValue) Get() interface{} {
return *s.value
}
// Set converts, and assigns, the IPNet argument string representation as the []net.IPNet value of this flag.
// If Set is called on a flag that already has a []net.IPNet assigned, the newly converted values will be appended.
func (s *ipNetSliceValue) Set(val string) error {
val = strings.TrimSpace(val)
_, n, err := net.ParseCIDR(val)
if err != nil {
return err
}
if n == nil {
return fmt.Errorf("invalid string being converted to CIDR: %s", val)
}
if !s.changed {
*s.value = []net.IPNet{*n}
} else {
*s.value = append(*s.value, *n)
}
s.changed = true
return nil
}
// Type returns a string that uniquely represents this flag's type.
func (s *ipNetSliceValue) Type() string {
return "ipNetSlice"
}
// String defines a "native" format for this net.IPNet slice flag value.
func (s *ipNetSliceValue) String() string {
if s.value == nil {
return "[]"
}
return fmt.Sprintf("%s", *s.value)
}
func (s *ipNetSliceValue) fromString(val string) (net.IPNet, error) {
_, cidr, err := net.ParseCIDR(val)
if err != nil {
return net.IPNet{}, err
}
return *cidr, nil
}
func (s *ipNetSliceValue) toString(val net.IPNet) string {
return val.String()
}
func (s *ipNetSliceValue) Append(val string) error {
i, err := s.fromString(val)
if err != nil {
return err
}
*s.value = append(*s.value, i)
return nil
}
func (s *ipNetSliceValue) Replace(val []string) error {
out := make([]net.IPNet, len(val))
for i, d := range val {
var err error
out[i], err = s.fromString(d)
if err != nil {
return err
}
}
*s.value = out
return nil
}
func (s *ipNetSliceValue) GetSlice() []string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = s.toString(d)
}
return out
}
// GetIPNetSlice returns the []net.IPNet value of a flag with the given name
func (fs *FlagSet) GetIPNetSlice(name string) ([]net.IPNet, error) {
val, err := fs.getFlagValue(name, "ipNetSlice")
if err != nil {
return []net.IPNet{}, err
}
return val.([]net.IPNet), nil
}
// MustGetIPNetSlice is like GetIPNetSlice, but panics on error.
func (fs *FlagSet) MustGetIPNetSlice(name string) []net.IPNet {
val, err := fs.GetIPNetSlice(name)
if err != nil {
panic(err)
}
return val
}
// IPNetSliceVar defines a []net.IPNet flag with specified name, default value, and usage string.
// The argument p points to a []net.IPNet variable in which to store the value of the flag.
func (fs *FlagSet) IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string, opts ...Opt) {
fs.Var(newIPNetSliceValue(value, p), name, usage, opts...)
}
// IPNetSliceVar defines a []net.IPNet flag with specified name, default value, and usage string.
// The argument p points to a []net.IPNet variable in which to store the value of the flag.
func IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string, opts ...Opt) {
CommandLine.IPNetSliceVar(p, name, value, usage, opts...)
}
// IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string.
// The return value is the address of a []net.IPNet variable that stores the value of the flag.
func (fs *FlagSet) IPNetSlice(name string, value []net.IPNet, usage string, opts ...Opt) *[]net.IPNet {
var p []net.IPNet
fs.IPNetSliceVar(&p, name, value, usage, opts...)
return &p
}
// IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string.
// The return value is the address of a []net.IPNet variable that stores the value of the flag.
func IPNetSlice(name string, value []net.IPNet, usage string, opts ...Opt) *[]net.IPNet {
return CommandLine.IPNetSlice(name, value, usage, opts...)
}