Skip to content

Commit

Permalink
fix(cmd/end.go): journal data being overwriting
Browse files Browse the repository at this point in the history
Entries would have their EndTime overwritten if multiple executions of the command `end` would be
ran. We are now fixing this by adding a check similar to what is done in the `start` command.
  • Loading branch information
deadpyxel committed Jun 18, 2024
1 parent 8b5b862 commit a68b1b6
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cmd/end.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func markDayAsFinished(cmd *cobra.Command, args []string) error {
// Get current date
now := time.Now()
currentDayId := now.Format("20060102")
dateStr := now.Format("2006-01-02")

// Load entries
journalPath := viper.GetString("journalPath")
Expand All @@ -40,13 +41,28 @@ func markDayAsFinished(cmd *cobra.Command, args []string) error {
}

// Get the index of the entry on the slice, -1 if not found
_, entryIndex := journal.FetchEntryByID(currentDayId, entries)
entry, idx := journal.FetchEntryByID(currentDayId, entries)

if entryIndex == -1 {
if idx == -1 {
return fmt.Errorf("No entry found for the current day")
}

entries[entryIndex].EndDay()
if !entry.EndTime.IsZero() {
fmt.Printf("There is already an EndTime for %s. Do you want to override it? (y/N): ", dateStr)
userInput, err := getUserInput()
if err != nil {
return err
}
if userInput != "y" {
fmt.Println("No changes made...")
return nil
}
entry.EndDay()
entries[idx] = *entry
fmt.Printf("Data for %s overwrote. Saving...", dateStr)
} else {
entries[idx].EndDay()
}

err = journal.SaveEntries(entries, journalPath)
if err != nil {
Expand Down

0 comments on commit a68b1b6

Please sign in to comment.