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) }