2021-02-01 00:10:16 +01:00
|
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Timestamp wrap to satisfy golang's flag interface.
|
|
|
|
|
type Timestamp struct {
|
|
|
|
|
timestamp *time.Time
|
|
|
|
|
hasBeenSet bool
|
|
|
|
|
layout string
|
2022-07-11 17:13:38 +02:00
|
|
|
|
location *time.Location
|
2021-02-01 00:10:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Timestamp constructor
|
|
|
|
|
func NewTimestamp(timestamp time.Time) *Timestamp {
|
|
|
|
|
return &Timestamp{timestamp: ×tamp}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the timestamp value directly
|
|
|
|
|
func (t *Timestamp) SetTimestamp(value time.Time) {
|
|
|
|
|
if !t.hasBeenSet {
|
|
|
|
|
t.timestamp = &value
|
|
|
|
|
t.hasBeenSet = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the timestamp string layout for future parsing
|
|
|
|
|
func (t *Timestamp) SetLayout(layout string) {
|
|
|
|
|
t.layout = layout
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 17:13:38 +02:00
|
|
|
|
// Set perceived timezone of the to-be parsed time string
|
|
|
|
|
func (t *Timestamp) SetLocation(loc *time.Location) {
|
|
|
|
|
t.location = loc
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 00:10:16 +01:00
|
|
|
|
// Parses the string value to timestamp
|
|
|
|
|
func (t *Timestamp) Set(value string) error {
|
2022-07-11 17:13:38 +02:00
|
|
|
|
var timestamp time.Time
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if t.location != nil {
|
|
|
|
|
timestamp, err = time.ParseInLocation(t.layout, value, t.location)
|
|
|
|
|
} else {
|
|
|
|
|
timestamp, err = time.Parse(t.layout, value)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 00:10:16 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.timestamp = ×tamp
|
|
|
|
|
t.hasBeenSet = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
|
|
|
|
func (t *Timestamp) String() string {
|
|
|
|
|
return fmt.Sprintf("%#v", t.timestamp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Value returns the timestamp value stored in the flag
|
|
|
|
|
func (t *Timestamp) Value() *time.Time {
|
|
|
|
|
return t.timestamp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get returns the flag structure
|
|
|
|
|
func (t *Timestamp) Get() interface{} {
|
|
|
|
|
return *t
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 09:56:38 +02:00
|
|
|
|
// clone timestamp
|
|
|
|
|
func (t *Timestamp) clone() *Timestamp {
|
|
|
|
|
tc := &Timestamp{
|
|
|
|
|
timestamp: nil,
|
|
|
|
|
hasBeenSet: t.hasBeenSet,
|
|
|
|
|
layout: t.layout,
|
|
|
|
|
location: nil,
|
|
|
|
|
}
|
|
|
|
|
if t.timestamp != nil {
|
|
|
|
|
tts := *t.timestamp
|
|
|
|
|
tc.timestamp = &tts
|
|
|
|
|
}
|
|
|
|
|
if t.location != nil {
|
|
|
|
|
loc := *t.location
|
|
|
|
|
tc.location = &loc
|
|
|
|
|
}
|
|
|
|
|
return tc
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 00:10:16 +01:00
|
|
|
|
// TakesValue returns true of the flag takes a value, otherwise false
|
|
|
|
|
func (f *TimestampFlag) TakesValue() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetUsage returns the usage string for the flag
|
|
|
|
|
func (f *TimestampFlag) GetUsage() string {
|
|
|
|
|
return f.Usage
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 20:49:12 +02:00
|
|
|
|
// GetCategory returns the category for the flag
|
|
|
|
|
func (f *TimestampFlag) GetCategory() string {
|
|
|
|
|
return f.Category
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 00:10:16 +01:00
|
|
|
|
// GetValue returns the flags value as string representation and an empty
|
|
|
|
|
// string if the flag takes no value at all.
|
|
|
|
|
func (f *TimestampFlag) GetValue() string {
|
2022-07-21 20:10:25 +02:00
|
|
|
|
if f.Value != nil && f.Value.timestamp != nil {
|
2021-02-01 00:10:16 +01:00
|
|
|
|
return f.Value.timestamp.String()
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-26 14:24:23 +02:00
|
|
|
|
// GetDefaultText returns the default text for this flag
|
|
|
|
|
func (f *TimestampFlag) GetDefaultText() string {
|
|
|
|
|
if f.DefaultText != "" {
|
|
|
|
|
return f.DefaultText
|
|
|
|
|
}
|
2022-10-18 09:56:38 +02:00
|
|
|
|
if f.defaultValue != nil && f.defaultValue.timestamp != nil {
|
|
|
|
|
return f.defaultValue.timestamp.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ""
|
2022-04-26 14:24:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetEnvVars returns the env vars for this flag
|
|
|
|
|
func (f *TimestampFlag) GetEnvVars() []string {
|
|
|
|
|
return f.EnvVars
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 00:10:16 +01:00
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
func (f *TimestampFlag) Apply(set *flag.FlagSet) error {
|
|
|
|
|
if f.Layout == "" {
|
|
|
|
|
return fmt.Errorf("timestamp Layout is required")
|
|
|
|
|
}
|
|
|
|
|
if f.Value == nil {
|
|
|
|
|
f.Value = &Timestamp{}
|
|
|
|
|
}
|
|
|
|
|
f.Value.SetLayout(f.Layout)
|
2022-07-11 17:13:38 +02:00
|
|
|
|
f.Value.SetLocation(f.Timezone)
|
2021-02-01 00:10:16 +01:00
|
|
|
|
|
2022-10-18 09:56:38 +02:00
|
|
|
|
f.defaultValue = f.Value.clone()
|
|
|
|
|
|
2022-05-25 20:49:12 +02:00
|
|
|
|
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
|
2021-02-01 00:10:16 +01:00
|
|
|
|
if err := f.Value.Set(val); err != nil {
|
2022-05-25 20:49:12 +02:00
|
|
|
|
return fmt.Errorf("could not parse %q as timestamp value from %s for flag %s: %s", val, source, f.Name, err)
|
2021-02-01 00:10:16 +01:00
|
|
|
|
}
|
|
|
|
|
f.HasBeenSet = true
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 09:05:50 +02:00
|
|
|
|
if f.Destination != nil {
|
|
|
|
|
*f.Destination = *f.Value
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 00:10:16 +01:00
|
|
|
|
for _, name := range f.Names() {
|
2022-03-16 12:55:03 +01:00
|
|
|
|
if f.Destination != nil {
|
|
|
|
|
set.Var(f.Destination, name, f.Usage)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 00:10:16 +01:00
|
|
|
|
set.Var(f.Value, name, f.Usage)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 00:48:35 +02:00
|
|
|
|
// Get returns the flag’s value in the given Context.
|
|
|
|
|
func (f *TimestampFlag) Get(ctx *Context) *time.Time {
|
|
|
|
|
return ctx.Timestamp(f.Name)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-01 16:18:50 +02:00
|
|
|
|
// RunAction executes flag action if set
|
|
|
|
|
func (f *TimestampFlag) RunAction(c *Context) error {
|
|
|
|
|
if f.Action != nil {
|
|
|
|
|
return f.Action(c, c.Timestamp(f.Name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 00:10:16 +01:00
|
|
|
|
// Timestamp gets the timestamp from a flag name
|
2022-04-26 14:24:23 +02:00
|
|
|
|
func (cCtx *Context) Timestamp(name string) *time.Time {
|
|
|
|
|
if fs := cCtx.lookupFlagSet(name); fs != nil {
|
2021-02-01 00:10:16 +01:00
|
|
|
|
return lookupTimestamp(name, fs)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetches the timestamp value from the local timestampWrap
|
|
|
|
|
func lookupTimestamp(name string, set *flag.FlagSet) *time.Time {
|
|
|
|
|
f := set.Lookup(name)
|
|
|
|
|
if f != nil {
|
|
|
|
|
return (f.Value.(*Timestamp)).Value()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|