This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
/
value.go
152 lines (117 loc) · 2.5 KB
/
value.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
package act
import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
// StringSlice implements flag.Getter interface for []string type.
type StringSlice []string
// Set sets flag's value by splitting provided comma separated string.
func (f *StringSlice) Set(s string) error {
if s == "" {
return nil
}
*f = strings.Split(s, ",")
return nil
}
// String formats flag's value.
func (f *StringSlice) String() string {
if f != nil {
if len(*f) == 0 {
return "[]"
}
return fmt.Sprintf("['%s']", strings.Join(*f, `','`))
}
return ""
}
// Get returns flag's value.
func (f *StringSlice) Get() interface{} {
return []string(*f)
}
// IntSlice implements flag.Getter interface for []int type.
type IntSlice []int
// Set sets flag's value by splitting provided comma separated string.
func (f *IntSlice) Set(s string) error {
if s == "" {
return nil
}
vs := strings.Split(s, ",")
*f = make([]int, 0, len(vs))
for _, v := range vs {
i, err := strconv.Atoi(v)
if err != nil {
*f = []int{}
return fmt.Errorf("parsing int: %w", err)
}
*f = append(*f, i)
}
return nil
}
// String formats flag's value.
func (f *IntSlice) String() string {
if f != nil {
if len(*f) == 0 {
return "[]"
}
s := make([]string, 0, len(*f))
for _, i := range *f {
s = append(s, fmt.Sprintf("%d", i))
}
return fmt.Sprintf("[%s]", strings.Join(s, `,`))
}
return ""
}
// Get returns flag's value.
func (f *IntSlice) Get() interface{} {
return []int(*f)
}
// URL implements flag.Getter interface for url.URL type.
type URL struct {
*url.URL
}
// Set sets flag's value by parsing provided url.
func (f *URL) Set(s string) error {
u, err := url.Parse(s)
if err != nil {
return fmt.Errorf("parsing url: %w", err)
}
f.URL = u
return nil
}
// String formats flag's value.
func (f *URL) String() string {
if f != nil && f.URL != nil {
return f.URL.String()
}
return ""
}
// Get returns flag's value.
func (f *URL) Get() interface{} {
return *f.URL
}
// Time implements flag.Getter interface for time.Time type.
type Time struct {
*time.Time
}
// Set sets flag's value by parsing provided url.
func (f *Time) Set(s string) error {
t, err := time.Parse(time.RFC3339, s)
if err != nil {
return fmt.Errorf("parsing time: %w", err)
}
f.Time = &t
return nil
}
// String formats flag's value.
func (f *Time) String() string {
if f != nil && f.Time != nil {
return f.Time.Format(time.RFC3339)
}
return ""
}
// Get returns flag's value.
func (f *Time) Get() interface{} {
return *f.Time
}