podman
186 строк · 5.3 Кб
1package pflag
2
3import (
4"fmt"
5"io"
6"net"
7"strings"
8)
9
10// -- ipSlice Value
11type ipSliceValue struct {
12value *[]net.IP
13changed bool
14}
15
16func newIPSliceValue(val []net.IP, p *[]net.IP) *ipSliceValue {
17ipsv := new(ipSliceValue)
18ipsv.value = p
19*ipsv.value = val
20return ipsv
21}
22
23// Set converts, and assigns, the comma-separated IP argument string representation as the []net.IP value of this flag.
24// If Set is called on a flag that already has a []net.IP assigned, the newly converted values will be appended.
25func (s *ipSliceValue) Set(val string) error {
26
27// remove all quote characters
28rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")
29
30// read flag arguments with CSV parser
31ipStrSlice, err := readAsCSV(rmQuote.Replace(val))
32if err != nil && err != io.EOF {
33return err
34}
35
36// parse ip values into slice
37out := make([]net.IP, 0, len(ipStrSlice))
38for _, ipStr := range ipStrSlice {
39ip := net.ParseIP(strings.TrimSpace(ipStr))
40if ip == nil {
41return fmt.Errorf("invalid string being converted to IP address: %s", ipStr)
42}
43out = append(out, ip)
44}
45
46if !s.changed {
47*s.value = out
48} else {
49*s.value = append(*s.value, out...)
50}
51
52s.changed = true
53
54return nil
55}
56
57// Type returns a string that uniquely represents this flag's type.
58func (s *ipSliceValue) Type() string {
59return "ipSlice"
60}
61
62// String defines a "native" format for this net.IP slice flag value.
63func (s *ipSliceValue) String() string {
64
65ipStrSlice := make([]string, len(*s.value))
66for i, ip := range *s.value {
67ipStrSlice[i] = ip.String()
68}
69
70out, _ := writeAsCSV(ipStrSlice)
71
72return "[" + out + "]"
73}
74
75func (s *ipSliceValue) fromString(val string) (net.IP, error) {
76return net.ParseIP(strings.TrimSpace(val)), nil
77}
78
79func (s *ipSliceValue) toString(val net.IP) string {
80return val.String()
81}
82
83func (s *ipSliceValue) Append(val string) error {
84i, err := s.fromString(val)
85if err != nil {
86return err
87}
88*s.value = append(*s.value, i)
89return nil
90}
91
92func (s *ipSliceValue) Replace(val []string) error {
93out := make([]net.IP, len(val))
94for i, d := range val {
95var err error
96out[i], err = s.fromString(d)
97if err != nil {
98return err
99}
100}
101*s.value = out
102return nil
103}
104
105func (s *ipSliceValue) GetSlice() []string {
106out := make([]string, len(*s.value))
107for i, d := range *s.value {
108out[i] = s.toString(d)
109}
110return out
111}
112
113func ipSliceConv(val string) (interface{}, error) {
114val = strings.Trim(val, "[]")
115// Empty string would cause a slice with one (empty) entry
116if len(val) == 0 {
117return []net.IP{}, nil
118}
119ss := strings.Split(val, ",")
120out := make([]net.IP, len(ss))
121for i, sval := range ss {
122ip := net.ParseIP(strings.TrimSpace(sval))
123if ip == nil {
124return nil, fmt.Errorf("invalid string being converted to IP address: %s", sval)
125}
126out[i] = ip
127}
128return out, nil
129}
130
131// GetIPSlice returns the []net.IP value of a flag with the given name
132func (f *FlagSet) GetIPSlice(name string) ([]net.IP, error) {
133val, err := f.getFlagType(name, "ipSlice", ipSliceConv)
134if err != nil {
135return []net.IP{}, err
136}
137return val.([]net.IP), nil
138}
139
140// IPSliceVar defines a ipSlice flag with specified name, default value, and usage string.
141// The argument p points to a []net.IP variable in which to store the value of the flag.
142func (f *FlagSet) IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) {
143f.VarP(newIPSliceValue(value, p), name, "", usage)
144}
145
146// IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash.
147func (f *FlagSet) IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) {
148f.VarP(newIPSliceValue(value, p), name, shorthand, usage)
149}
150
151// IPSliceVar defines a []net.IP flag with specified name, default value, and usage string.
152// The argument p points to a []net.IP variable in which to store the value of the flag.
153func IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) {
154CommandLine.VarP(newIPSliceValue(value, p), name, "", usage)
155}
156
157// IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash.
158func IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) {
159CommandLine.VarP(newIPSliceValue(value, p), name, shorthand, usage)
160}
161
162// IPSlice defines a []net.IP flag with specified name, default value, and usage string.
163// The return value is the address of a []net.IP variable that stores the value of that flag.
164func (f *FlagSet) IPSlice(name string, value []net.IP, usage string) *[]net.IP {
165p := []net.IP{}
166f.IPSliceVarP(&p, name, "", value, usage)
167return &p
168}
169
170// IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash.
171func (f *FlagSet) IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP {
172p := []net.IP{}
173f.IPSliceVarP(&p, name, shorthand, value, usage)
174return &p
175}
176
177// IPSlice defines a []net.IP flag with specified name, default value, and usage string.
178// The return value is the address of a []net.IP variable that stores the value of the flag.
179func IPSlice(name string, value []net.IP, usage string) *[]net.IP {
180return CommandLine.IPSliceP(name, "", value, usage)
181}
182
183// IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash.
184func IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP {
185return CommandLine.IPSliceP(name, shorthand, value, usage)
186}
187