From d8b73a0ff657629f82130d4e23ff6b2f7eea0ea6 Mon Sep 17 00:00:00 2001 From: Robson Cruz Date: Tue, 31 Oct 2023 20:02:34 -0300 Subject: [PATCH] feat(cdm/report): add `report` command this will show the data for the current workday entry fix(journal): update `String` method to handle zero values docs(cmd/report): add documention for `report` command --- cmd/report.go | 39 ++++++++++++++++++++++++++++----------- internal/journal.go | 11 ++++++++--- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/cmd/report.go b/cmd/report.go index d932622..828209c 100644 --- a/cmd/report.go +++ b/cmd/report.go @@ -1,28 +1,45 @@ /* Copyright © 2023 NAME HERE - */ package cmd import ( "fmt" + "time" + journal "github.com/deadpyxel/workday/internal" "github.com/spf13/cobra" ) // reportCmd represents the report command var reportCmd = &cobra.Command{ Use: "report", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("report called") - }, + Short: "Reports the workday entry for the current day", + Long: `The report command is used to print out the workday entry for the current day. + +It loads the existing jorunal entries from the file and fetches the entry for the current day. +If there is no entry for the current day, it returns an error. +Otherwise, it prints out the entry.`, + RunE: reportWorkDay, +} + +// reportWorkDay reports the workday entry for the current day. +// It first loads the existing journal entries from the file. +// If there is no entry for the current day, it returns and error. +// Otherwise, it prints out the entry. +func reportWorkDay(cmd *cobra.Command, args []string) error { + journalEntries, err := journal.LoadEntries("journal.json") + if err != nil { + return err + } + now := time.Now() + currenctDayId := now.Format("20060102") + currentEntry, _ := journal.FetchEntryByID(currenctDayId, journalEntries) + if currentEntry == nil { + return fmt.Errorf("Could not find any entry for the current day.") + } + fmt.Println(currentEntry) + return nil } func init() { diff --git a/internal/journal.go b/internal/journal.go index 328c6f4..87ddaeb 100644 --- a/internal/journal.go +++ b/internal/journal.go @@ -21,13 +21,18 @@ func NewJournalEntry() *JournalEntry { func (j *JournalEntry) String() string { start := j.StartTime.Format("15:04:05") end := j.EndTime.Format("15:04:05") - totalTime := j.EndTime.Sub(j.StartTime) - timeStr := fmt.Sprintf("Start: %s | End: %s | Time: %s", start, end, totalTime.String()) + totalTime := j.EndTime.Sub(j.StartTime).String() + if j.EndTime.IsZero() { + end = "Not yet closed" + totalTime = "N/A" + } + timeStr := fmt.Sprintf("Start: %s | End: %s | Time: %s", start, end, totalTime) notes := "" for _, note := range j.Notes { notes += fmt.Sprintf("- %s\n", note) } - return fmt.Sprintf("\n%s\n\n%s", timeStr, notes) + headerStr := fmt.Sprintf("Date: %s", j.StartTime.Format("2006-01-02")) + return fmt.Sprintf("%s\n%s\n\n%s", headerStr, timeStr, notes) } func (j *JournalEntry) AddNote(note string) {