Debian Codegraph @

vendor/github.com/urfave/cli/v2/flag_timestamp.go

raw
1 package cli
2
3 import (
4 "flag"
5 "fmt"
6 "time"
7 )
8
9 // Timestamp wrap to satisfy golang's flag interface.
10 type Timestamp struct {
11 timestamp *time.Time
12 hasBeenSet bool
13 layout string
14 location *time.Location
15 }
16
17 // Timestamp constructor
18 func NewTimestamp(timestamp time.Time) *Timestamp {
19 return &Timestamp{timestamp: &timestamp}
20 }
21
22 // Set the timestamp value directly
23 func (t *Timestamp) SetTimestamp(value time.Time) {
24 if !t.hasBeenSet {
25 t.timestamp = &value
26 t.hasBeenSet = true
27 }
28 }
29
30 // Set the timestamp string layout for future parsing
31 func (t *Timestamp) SetLayout(layout string) {
32 t.layout = layout
33 }
34
35 // Set perceived timezone of the to-be parsed time string
36 func (t *Timestamp) SetLocation(loc *time.Location) {
37 t.location = loc
38 }
39
40 // Parses the string value to timestamp
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 = &timestamp
56 t.hasBeenSet = true
57 return nil
58 }
59
60 // String returns a readable representation of this value (for usage defaults)
61 func (t *Timestamp) String() string {
62 return fmt.Sprintf("%#v", t.timestamp)
63 }
64
65 // Value returns the timestamp value stored in the flag
66 func (t *Timestamp) Value() *time.Time {
67 return t.timestamp
68 }
69
70 // Get returns the flag structure
71 func (t *Timestamp) Get() interface{} {
72 return *t
73 }
74
75 // clone timestamp
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 // TakesValue returns true of the flag takes a value, otherwise false
95 func (f *TimestampFlag) TakesValue() bool {
96 return true
97 }
98
99 // GetUsage returns the usage string for the flag
100 func (f *TimestampFlag) GetUsage() string {
101 return f.Usage
102 }
103
104 // GetCategory returns the category for the flag
105 func (f *TimestampFlag) GetCategory() string {
106 return f.Category
107 }
108
109 // GetValue returns the flags value as string representation and an empty
110 // string if the flag takes no value at all.
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 // GetDefaultText returns the default text for this flag
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 // GetEnvVars returns the env vars for this flag
136 func (f *TimestampFlag) GetEnvVars() []string {
137 return f.EnvVars
138 }
139
140 // Apply populates the flag given the flag set and environment
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 // Get returns the flag’s value in the given Context.
177 func (f *TimestampFlag) Get(ctx *Context) *time.Time {
178 return ctx.Timestamp(f.Name)
179 }
180
181 // RunAction executes flag action if set
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 // Timestamp gets the timestamp from a flag name
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 // Fetches the timestamp value from the local timestampWrap
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 }