Skip to content

Commit

Permalink
Merge pull request #1831 from dearchap/fix_ts
Browse files Browse the repository at this point in the history
Fix timestamp get from cmd
  • Loading branch information
dearchap committed Dec 4, 2023
2 parents 1f0e15c + e1c363f commit 6e7906d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
38 changes: 38 additions & 0 deletions command_test.go
Expand Up @@ -3254,6 +3254,44 @@ func TestCommand_Duration(t *testing.T) {
r.Equal(13*time.Second, cmd.Duration("top-flag"))
}

func TestCommand_Timestamp(t *testing.T) {
t1 := time.Time{}.Add(12 * time.Second)
t2 := time.Time{}.Add(13 * time.Second)

cmd := &Command{
Name: "hello",
Flags: []Flag{
&TimestampFlag{
Name: "myflag",
Value: t1,
},
},
Action: func(ctx context.Context, c *Command) error {
return nil
},
}

pCmd := &Command{
Flags: []Flag{
&TimestampFlag{
Name: "top-flag",
Value: t2,
},
},
Commands: []*Command{
cmd,
},
}

if err := pCmd.Run(context.Background(), []string{"foo", "hello"}); err != nil {
t.Error(err)
}

r := require.New(t)
r.Equal(t1, cmd.Timestamp("myflag"))
r.Equal(t2, cmd.Timestamp("top-flag"))
}

func TestCommand_String(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.String("myflag", "hello world", "doc")
Expand Down
26 changes: 6 additions & 20 deletions flag_timestamp.go
@@ -1,7 +1,6 @@
package cli

import (
"flag"
"fmt"
"time"
)
Expand Down Expand Up @@ -85,25 +84,12 @@ func (t *timestampValue) Get() any {
}

// Timestamp gets the timestamp from a flag name
func (cmd *Command) Timestamp(name string) *time.Time {
if fs := cmd.lookupFlagSet(name); fs != nil {
return lookupTimestamp(name, fs, cmd.Name)
func (cmd *Command) Timestamp(name string) time.Time {
if v, ok := cmd.Value(name).(time.Time); ok {
tracef("time.Time available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmd.Name)
return v
}
return nil
}

// Fetches the timestamp value from the local timestampWrap
func lookupTimestamp(name string, set *flag.FlagSet, cmdName string) *time.Time {
fl := set.Lookup(name)
if fl != nil {
if tv, ok := fl.Value.(*timestampValue); ok {
v := tv.Value()

tracef("timestamp available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmdName)
return v
}
}

tracef("timestamp NOT available for flag name %[1]q (cmd=%[2]q)", name, cmdName)
return nil
tracef("time.Time NOT available for flag name %[1]q (cmd=%[2]q)", name, cmd.Name)
return time.Time{}
}
2 changes: 1 addition & 1 deletion godoc-current.txt
Expand Up @@ -456,7 +456,7 @@ func (cmd *Command) StringSlice(name string) []string
StringSlice looks up the value of a local StringSliceFlag, returns nil if
not found

func (cmd *Command) Timestamp(name string) *time.Time
func (cmd *Command) Timestamp(name string) time.Time
Timestamp gets the timestamp from a flag name

func (cmd *Command) ToFishCompletion() (string, error)
Expand Down
2 changes: 1 addition & 1 deletion testdata/godoc-v3.x.txt
Expand Up @@ -456,7 +456,7 @@ func (cmd *Command) StringSlice(name string) []string
StringSlice looks up the value of a local StringSliceFlag, returns nil if
not found

func (cmd *Command) Timestamp(name string) *time.Time
func (cmd *Command) Timestamp(name string) time.Time
Timestamp gets the timestamp from a flag name

func (cmd *Command) ToFishCompletion() (string, error)
Expand Down

0 comments on commit 6e7906d

Please sign in to comment.