Skip to content

Commit

Permalink
017 Specific arguments for today and this week (#20)
Browse files Browse the repository at this point in the history
* 017 Specific arguments for today and this week

* 017 Move argument validation
  • Loading branch information
PossibleLlama authored Nov 22, 2020
1 parent 3882b69 commit 3058f9e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/cmd/print.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"time"

Expand All @@ -10,20 +11,32 @@ import (

var startDate time.Time
var startDateString string
var today bool
var thisWeek bool

// printCmd represents the print command
var printCmd = &cobra.Command{
Use: "print",
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
Expand All @@ -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")
}
12 changes: 12 additions & 0 deletions src/helpers/dates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3058f9e

Please sign in to comment.