Skip to content

Commit

Permalink
feat(cmd/report_month): add optional flag for month filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
deadpyxel committed May 2, 2024
1 parent 894d0ab commit e1983cd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 16 additions & 3 deletions cmd/report_month.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -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
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions internal/journal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e1983cd

Please sign in to comment.