...
1 package cli
2
3 import (
4 "flag"
5 "fmt"
6 "time"
7 )
8
9
10 type Timestamp struct {
11 timestamp *time.Time
12 hasBeenSet bool
13 layout string
14 location *time.Location
15 }
16
17
18 func NewTimestamp(timestamp time.Time) *Timestamp {
19 return &Timestamp{timestamp: ×tamp}
20 }
21
22
23 func (t *Timestamp) SetTimestamp(value time.Time) {
24 if !t.hasBeenSet {
25 t.timestamp = &value
26 t.hasBeenSet = true
27 }
28 }
29
30
31 func (t *Timestamp) SetLayout(layout string) {
32 t.layout = layout
33 }
34
35
36 func (t *Timestamp) SetLocation(loc *time.Location) {
37 t.location = loc
38 }
39
40
41 func (t *Timestamp) Set(value string) error {
42 var timestamp time.Time
43 var err error
44
45 if t.location != nil {
46 timestamp, err = time.ParseInLocation(t.layout, value, t.location)
47 } else {
48 timestamp, err = time.Parse(t.layout, value)
49 }
50
51 if err != nil {
52 return err
53 }
54
55 t.timestamp = ×tamp
56 t.hasBeenSet = true
57 return nil
58 }
59
60
61 func (t *Timestamp) String() string {
62 return fmt.Sprintf("%#v", t.timestamp)
63 }
64
65
66 func (t *Timestamp) Value() *time.Time {
67 return t.timestamp
68 }
69
70
71 func (t *Timestamp) Get() interface{} {
72 return *t
73 }
74
75
76 func (t *Timestamp) clone() *Timestamp {
77 tc := &Timestamp{
78 timestamp: nil,
79 hasBeenSet: t.hasBeenSet,
80 layout: t.layout,
81 location: nil,
82 }
83 if t.timestamp != nil {
84 tts := *t.timestamp
85 tc.timestamp = &tts
86 }
87 if t.location != nil {
88 loc := *t.location
89 tc.location = &loc
90 }
91 return tc
92 }
93
94
95 func (f *TimestampFlag) TakesValue() bool {
96 return true
97 }
98
99
100 func (f *TimestampFlag) GetUsage() string {
101 return f.Usage
102 }
103
104
105 func (f *TimestampFlag) GetCategory() string {
106 return f.Category
107 }
108
109
110
111 func (f *TimestampFlag) GetValue() string {
112 if f.Value != nil && f.Value.timestamp != nil {
113 return f.Value.timestamp.String()
114 }
115 return ""
116 }
117
118
119 func (f *TimestampFlag) GetDefaultText() string {
120 if f.DefaultText != "" {
121 return f.DefaultText
122 }
123 val := f.Value
124 if f.defaultValueSet {
125 val = f.defaultValue
126 }
127
128 if val != nil && val.timestamp != nil {
129 return val.timestamp.String()
130 }
131
132 return ""
133 }
134
135
136 func (f *TimestampFlag) GetEnvVars() []string {
137 return f.EnvVars
138 }
139
140
141 func (f *TimestampFlag) Apply(set *flag.FlagSet) error {
142 if f.Layout == "" {
143 return fmt.Errorf("timestamp Layout is required")
144 }
145 if f.Value == nil {
146 f.Value = &Timestamp{}
147 }
148 f.Value.SetLayout(f.Layout)
149 f.Value.SetLocation(f.Timezone)
150
151 f.defaultValue = f.Value.clone()
152 f.defaultValueSet = true
153
154 if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
155 if err := f.Value.Set(val); err != nil {
156 return fmt.Errorf("could not parse %q as timestamp value from %s for flag %s: %s", val, source, f.Name, err)
157 }
158 f.HasBeenSet = true
159 }
160
161 if f.Destination != nil {
162 *f.Destination = *f.Value
163 }
164
165 for _, name := range f.Names() {
166 if f.Destination != nil {
167 set.Var(f.Destination, name, f.Usage)
168 continue
169 }
170
171 set.Var(f.Value, name, f.Usage)
172 }
173 return nil
174 }
175
176
177 func (f *TimestampFlag) Get(ctx *Context) *time.Time {
178 return ctx.Timestamp(f.Name)
179 }
180
181
182 func (f *TimestampFlag) RunAction(c *Context) error {
183 if f.Action != nil {
184 return f.Action(c, c.Timestamp(f.Name))
185 }
186
187 return nil
188 }
189
190
191 func (cCtx *Context) Timestamp(name string) *time.Time {
192 if fs := cCtx.lookupFlagSet(name); fs != nil {
193 return lookupTimestamp(name, fs)
194 }
195 return nil
196 }
197
198
199 func lookupTimestamp(name string, set *flag.FlagSet) *time.Time {
200 f := set.Lookup(name)
201 if f != nil {
202 return (f.Value.(*Timestamp)).Value()
203 }
204 return nil
205 }
206
View as plain text