From e1983cd3a650c048e7d1c439a3764046101a1268 Mon Sep 17 00:00:00 2001 From: Robson Cruz Date: Thu, 2 May 2024 16:30:37 -0300 Subject: [PATCH] feat(cmd/report_month): add optional flag for month filtering --- cmd/report_month.go | 19 ++++++++++++++++--- internal/journal/utils.go | 4 ++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cmd/report_month.go b/cmd/report_month.go index a43e750..435c654 100644 --- a/cmd/report_month.go +++ b/cmd/report_month.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "time" @@ -35,15 +36,26 @@ func reportMonth(cmd *cobra.Command, args []string) error { if err != nil { return err } - now := time.Now() - currentMonth, err := journal.FetchEntriesByMonthDate(entries, now) + + // Check if the month flag has been set + monthFlag, _ := cmd.Flags().GetString("month") + var monthFilter time.Time + if monthFlag != "" { + monthFilter, err = time.Parse("2006-01", monthFlag) + if err != nil { + return errors.New("invalid month format, expected YYYY-MM") + } + } else { + monthFilter = time.Now() + } + currentMonth, err := journal.FetchEntriesByMonthDate(entries, monthFilter) if err != nil { return err } for _, entry := range currentMonth { fmt.Printf("%s\n---\n", entry.String()) } - month := now.Format("January 2006") + month := monthFilter.Format("January 2006") lunchTime, err := time.ParseDuration(viper.GetString("lunchTime")) if err != nil { return err @@ -59,5 +71,6 @@ func reportMonth(cmd *cobra.Command, args []string) error { } func init() { + reportMonthCmd.Flags().StringP("month", "m", "", "Specify the month and year in the format YYYY-MM") reportCmd.AddCommand(reportMonthCmd) } diff --git a/internal/journal/utils.go b/internal/journal/utils.go index e58490e..9c880b7 100644 --- a/internal/journal/utils.go +++ b/internal/journal/utils.go @@ -81,14 +81,14 @@ func FetchEntriesByWeekDate(journalEntries []JournalEntry, currentDate time.Time // log.Fatal(err) // } // fmt.Println(currentMonthEntries) // Prints the entries for the current week -func FetchEntriesByMonthDate(journalEntries []JournalEntry, currentDate time.Time) ([]JournalEntry, error) { +func FetchEntriesByMonthDate(journalEntries []JournalEntry, filterDate time.Time) ([]JournalEntry, error) { if len(journalEntries) == 0 { return nil, fmt.Errorf("No entries were passed") } var currentMonthEntries []JournalEntry // Get the current year and month. - currentYear, currentMonth := currentDate.Year(), currentDate.Month() + currentYear, currentMonth := filterDate.Year(), filterDate.Month() for _, entry := range journalEntries { // get the year and month of the entry.