/
flagutil.go
163 lines (128 loc) · 4.24 KB
/
flagutil.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
156
157
158
159
160
161
162
163
// Package flagutil provides a collection of types implementing the flag.Value interface.
package flagutil
import (
"flag"
"net"
"net/url"
"strings"
"time"
)
// NetworkAddress is a flag.Value that represents a network address.
// here a network address means anything that can be parsed by `net.SplitHostPort`.
type NetworkAddress string
// Set implements the flag.Value interface.
// It parses the string using `net.SplitHostPort` for validation.
func (a *NetworkAddress) Set(s string) error {
_, _, err := net.SplitHostPort(s)
if err != nil {
return err
}
*a = NetworkAddress(s)
return nil
}
// String implements the flag.Value interface. It returns the slice as a comma-separated string.
func (a NetworkAddress) String() string { return string(a) }
// NetworkAddresses is a slice of string that have been validated as valid network addresses.
// Use it as a flag value when you want to pass a comma-separated list of strings to a flag
// and have it to be automatically parsed into a slice and validated as valid network addresses.
type NetworkAddresses []string
// Set implements the flag.Value interface. It parses the string as a comma-separated string.
// Additionally, each value is then passed to net.SplitHostPort for validation that it's a correct network address.
func (a *NetworkAddresses) Set(s string) error {
for _, t := range strings.Split(s, ",") {
_, _, err := net.SplitHostPort(t)
if err != nil {
return err
}
*a = append(*a, t)
}
return nil
}
// String implements the flag.Value interface. It returns the slice as a comma-separated string.
func (a NetworkAddresses) String() string { return strings.Join(a, ",") }
// StringSlice returns the network addresses as a slice of string.
func (a NetworkAddresses) StringSlice() []string {
var res []string
for _, el := range a {
res = append(res, el)
}
return res
}
// Strings is a slice of string.
// Use it as a flag value when you want to pass a comma-separated list of strings to a flag
// and have it to be automatically parsed into a slice.
type Strings []string
// Set implements the flag.Value interface. It parses the string as a comma-separated string.
func (s *Strings) Set(str string) error {
for _, t := range strings.Split(str, ",") {
*s = append(*s, t)
}
return nil
}
// String implements the flag.Value interface. It returns the slice as a comma-separated string.
func (s Strings) String() string {
return strings.Join(s, ",")
}
// URL is a wrapper around a url.URL type.
// Use it as a flag value when you want to validate a flag as a valid URL.
type URL struct {
url.URL
parsed bool
}
// Set implements the flag.Value interface. It parses the string as a url.URL value.
func (u *URL) Set(s string) error {
url, err := url.Parse(s)
if err != nil {
return err
}
u.URL = *url
u.parsed = true
return nil
}
// String implements the flag.Value interface. It returns the underlying url.URL as a string.
func (u URL) String() string { return u.URL.String() }
// IsValid returns true if the URL object is valid, false otherwise. Valid here means it has correctly parsed an URL.
func (u URL) IsValid() bool { return u.parsed }
// URLs is a slice of url.URL.
// Use it as a flag value when you want to pass a comma-separated list of strings to a flag
// and have it to be automatically parsed into a slice and validated as valid url.URL.
type URLs []url.URL
// Set implements the flag.Value interface. It parses the string as a comma-separated string.
func (u *URLs) Set(s string) error {
for _, t := range strings.Split(s, ",") {
url, err := url.Parse(t)
if err != nil {
return err
}
*u = append(*u, *url)
}
return nil
}
// String implements the flag.Value interface. It returns the string representation of each url.URL as a comma-separated string.
func (u URLs) String() string {
var r []string
for _, url := range u {
r = append(r, url.String())
}
return strings.Join(r, ",")
}
type Duration struct {
time.Duration
}
func (d *Duration) Set(s string) error {
tmp, err := time.ParseDuration(s)
if err != nil {
return err
}
d.Duration = tmp
return nil
}
func (d Duration) String() string {
return d.Duration.String()
}
var (
_ flag.Value = (*NetworkAddresses)(nil)
_ flag.Value = (*Strings)(nil)
_ flag.Value = (*URLs)(nil)
_ flag.Value = (*Duration)(nil)
)