Skip to content

Commit

Permalink
feat(cdm/report): add report command
Browse files Browse the repository at this point in the history
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
  • Loading branch information
deadpyxel committed Oct 31, 2023
1 parent 4ec34b4 commit d8b73a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
39 changes: 28 additions & 11 deletions cmd/report.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
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() {
Expand Down
11 changes: 8 additions & 3 deletions internal/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d8b73a0

Please sign in to comment.