From b6b5486cf0e2e897a79be0de363f17bb4de66583 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 22 Sep 2017 12:43:23 +0200 Subject: [PATCH] Support "today" and "now" in date input Signed-off-by: Knut Ahlers --- cmd/add.go | 2 +- cmd/helpers.go | 11 +++++++++++ cmd/patch.go | 3 +-- cmd/remove.go | 2 +- cmd/show.go | 2 +- cmd/tag.go | 3 +-- 6 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 cmd/helpers.go diff --git a/cmd/add.go b/cmd/add.go index 511b0e0..3ea59b2 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -18,7 +18,7 @@ var addCmd = &cobra.Command{ return fmt.Errorf("Please supply required arguments") } - day, err := time.Parse("2006-01-02", args[0]) + day, err := parseTime("2006-01-02", args[0]) if err != nil { return fmt.Errorf("'day' parameter seems to have a wrong format: %s", err) } diff --git a/cmd/helpers.go b/cmd/helpers.go new file mode 100644 index 0000000..12d316a --- /dev/null +++ b/cmd/helpers.go @@ -0,0 +1,11 @@ +package cmd + +import "time" + +func parseTime(format, input string) (time.Time, error) { + if input == "today" || input == "now" { + input = time.Now().Format(format) + } + + return time.Parse(format, input) +} diff --git a/cmd/patch.go b/cmd/patch.go index cf52758..fb996dc 100644 --- a/cmd/patch.go +++ b/cmd/patch.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "strings" - "time" "github.com/Luzifer/worktime/schema" "github.com/spf13/cobra" @@ -19,7 +18,7 @@ var patchCmd = &cobra.Command{ return fmt.Errorf("Please supply required arguments") } - day, err := time.Parse("2006-01-02", args[0]) + day, err := parseTime("2006-01-02", args[0]) if err != nil { return fmt.Errorf("'day' parameter seems to have a wrong format: %s", err) } diff --git a/cmd/remove.go b/cmd/remove.go index 1d5204d..00e80e0 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -28,7 +28,7 @@ var removeCmd = &cobra.Command{ return fmt.Errorf("Please supply required arguments") } - day, err := time.Parse("2006-01-02", inDay) + day, err := parseTime("2006-01-02", inDay) if err != nil { return fmt.Errorf("'day' parameter seems to have a wrong format: %s", err) } diff --git a/cmd/show.go b/cmd/show.go index 9249d15..0b2ffab 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -21,7 +21,7 @@ var showCmd = &cobra.Command{ args = []string{time.Now().Format("2006-01-02")} } - day, err := time.Parse("2006-01-02", args[0]) + day, err := parseTime("2006-01-02", args[0]) if err != nil { return fmt.Errorf("'day' parameter seems to have a wrong format: %s", err) } diff --git a/cmd/tag.go b/cmd/tag.go index 0d84991..7f29040 100644 --- a/cmd/tag.go +++ b/cmd/tag.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "strings" - "time" "github.com/Luzifer/worktime/schema" "github.com/spf13/cobra" @@ -25,7 +24,7 @@ var tagCmd = &cobra.Command{ return fmt.Errorf("Please supply required arguments") } - day, err := time.Parse("2006-01-02", args[0]) + day, err := parseTime("2006-01-02", args[0]) if err != nil { return fmt.Errorf("'day' parameter seems to have a wrong format: %s", err) }