You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
urfave-cli/flag_timestamp.go

181 lines
3.9 KiB

4 years ago
package cli
import (
"flag"
"fmt"
"time"
)
4 years ago
// Timestamp wrap to satisfy golang's flag interface.
type Timestamp struct {
4 years ago
timestamp *time.Time
4 years ago
hasBeenSet bool
4 years ago
layout string
location *time.Location
4 years ago
}
4 years ago
// Timestamp constructor
func NewTimestamp(timestamp time.Time) *Timestamp {
return &Timestamp{timestamp: &timestamp}
}
4 years ago
// Set the timestamp value directly
4 years ago
func (t *Timestamp) SetTimestamp(value time.Time) {
4 years ago
if !t.hasBeenSet {
t.timestamp = &value
t.hasBeenSet = true
}
}
4 years ago
4 years ago
// Set the timestamp string layout for future parsing
4 years ago
func (t *Timestamp) SetLayout(layout string) {
4 years ago
t.layout = layout
}
// Set perceived timezone of the to-be parsed time string
func (t *Timestamp) SetLocation(loc *time.Location) {
t.location = loc
}
4 years ago
// Parses the string value to timestamp
4 years ago
func (t *Timestamp) Set(value string) error {
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)
}
4 years ago
if err != nil {
return err
}
t.timestamp = &timestamp
4 years ago
t.hasBeenSet = true
4 years ago
return nil
}
// String returns a readable representation of this value (for usage defaults)
4 years ago
func (t *Timestamp) String() string {
4 years ago
return fmt.Sprintf("%#v", t.timestamp)
}
// Value returns the timestamp value stored in the flag
4 years ago
func (t *Timestamp) Value() *time.Time {
4 years ago
return t.timestamp
}
// Get returns the flag structure
4 years ago
func (t *Timestamp) Get() interface{} {
4 years ago
return *t
}
// 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
}
4 years ago
// 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 {
if f.Value != nil && f.Value.timestamp != nil {
4 years ago
return f.Value.timestamp.String()
}
return ""
4 years ago
}
// GetDefaultText returns the default text for this flag
func (f *TimestampFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
if f.defaultValue != nil && f.defaultValue.timestamp != nil {
return f.defaultValue.timestamp.String()
}
return ""
}
4 years ago
// Apply populates the flag given the flag set and environment
func (f *TimestampFlag) Apply(set *flag.FlagSet) error {
4 years ago
if f.Layout == "" {
return fmt.Errorf("timestamp Layout is required")
}
if f.Value == nil {
f.Value = &Timestamp{}
}
4 years ago
f.Value.SetLayout(f.Layout)
f.Value.SetLocation(f.Timezone)
4 years ago
f.defaultValue = f.Value.clone()
if f.Destination != nil {
f.Destination.SetLayout(f.Layout)
f.Destination.SetLocation(f.Timezone)
}
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
4 years ago
if err := f.Value.Set(val); err != nil {
return fmt.Errorf("could not parse %q as timestamp value from %s for flag %s: %s", val, source, f.Name, err)
4 years ago
}
f.HasBeenSet = true
}
4 years ago
for _, name := range f.Names() {
if f.Destination != nil {
set.Var(f.Destination, name, f.Usage)
continue
}
4 years ago
set.Var(f.Value, name, f.Usage)
4 years ago
}
return nil
}
// Get returns the flags value in the given Context.
func (f *TimestampFlag) Get(ctx *Context) *time.Time {
return ctx.Timestamp(f.Name)
}
// 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
}
4 years ago
// Timestamp gets the timestamp from a flag name
func (cCtx *Context) Timestamp(name string) *time.Time {
if fs := cCtx.lookupFlagSet(name); fs != nil {
4 years ago
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 {
4 years ago
return (f.Value.(*Timestamp)).Value()
4 years ago
}
return nil
}