From 3058f9e437bf5b5259861260f99a3eb5c88e5ae5 Mon Sep 17 00:00:00 2001 From: Chris Collier <29115613+PossibleLlama@users.noreply.github.com> Date: Sun, 22 Nov 2020 17:32:04 +0000 Subject: [PATCH] 017 Specific arguments for today and this week (#20) * 017 Specific arguments for today and this week * 017 Move argument validation --- src/cmd/print.go | 40 ++++++++++++++++++++++++++++++++-------- src/helpers/dates.go | 12 ++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/cmd/print.go b/src/cmd/print.go index ab2eaf0..6d9355f 100644 --- a/src/cmd/print.go +++ b/src/cmd/print.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "time" @@ -10,6 +11,8 @@ import ( var startDate time.Time var startDateString string +var today bool +var thisWeek bool // printCmd represents the print command var printCmd = &cobra.Command{ @@ -17,13 +20,23 @@ var printCmd = &cobra.Command{ Short: "Print all worklogs since provided date", Long: `Prints all worklogs to console that have been created since the start provided date.`, - RunE: func(cmd *cobra.Command, args []string) error { - startDateAnytime, err := helpers.GetStringAsDateTime(startDateString) - startDate = helpers.Midnight(startDateAnytime) - if err != nil { - return err + Args: func(cmd *cobra.Command, args []string) error { + if len(startDateString) != 0 { + startDateAnytime, err := helpers.GetStringAsDateTime(startDateString) + if err != nil { + return err + } + startDate = helpers.Midnight(startDateAnytime) + } else if today { + startDate = helpers.Midnight(time.Now()) + } else if thisWeek { + startDate = helpers.Midnight(helpers.GetPreviousMonday(time.Now())) + } else { + return errors.New("one flag is required") } - + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { worklogs, _, err := wlService.GetWorklogsSince(startDate) if err != nil { return err @@ -42,7 +55,18 @@ func init() { printCmd.Flags().StringVar( &startDateString, "startDate", - helpers.TimeFormat(time.Now()), + "", "Date from which to find worklogs") - printCmd.MarkFlagRequired("startDate") + printCmd.Flags().BoolVarP( + &today, + "today", + "", + false, + "Print today's work") + printCmd.Flags().BoolVarP( + &thisWeek, + "thisWeek", + "", + false, + "Prints this weeks work") } diff --git a/src/helpers/dates.go b/src/helpers/dates.go index 013a906..d9cdc8b 100644 --- a/src/helpers/dates.go +++ b/src/helpers/dates.go @@ -58,3 +58,15 @@ func Midnight(t time.Time) time.Time { year, month, day := t.Date() return time.Date(year, month, day, 0, 0, 0, 0, t.Location()) } + +// GetPreviousMonday getting the most recent Monday +func GetPreviousMonday(originalTime time.Time) time.Time { + t := originalTime + for i := 0; i <= 6; i++ { + if t.Weekday() == time.Monday { + return t + } + t = t.AddDate(0, 0, -1) + } + return originalTime +}