Skip to content

Commit

Permalink
feat(cmd/note): add note edit command
Browse files Browse the repository at this point in the history
  • Loading branch information
deadpyxel committed Nov 17, 2023
1 parent c5cd71f commit 3ced2c2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
10 changes: 0 additions & 10 deletions cmd/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,4 @@ func addNoteToCurrentDay(cmd *cobra.Command, args []string) error {

func init() {
rootCmd.AddCommand(noteCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// noteCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// noteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
63 changes: 63 additions & 0 deletions cmd/note_edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"fmt"
"strconv"
"time"

journal "github.com/deadpyxel/workday/internal"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// noteEditCmd represents the note edit command
var noteEditCmd = &cobra.Command{
Use: "edit [index] [new note]",
Args: cobra.ExactArgs(2),
Short: "Edits a note in the current workday entry",
Long: `The note edit command is used to edit a note in the current workday entry.
It requires two arguments: the index of the note to be edited and the new note text. The index must be provided as a number.
If there is no entry for the current day, the command will print an error message and return an error.
Otherwise, it will edit the note at the specified index and save the updated journal entries back to the file`,
RunE: editNoteInCurrentDay,
}

func editNoteInCurrentDay(cmd *cobra.Command, args []string) error {
journalPath := viper.GetString("journalPath")
journalEntries, err := journal.LoadEntries(journalPath)
if err != nil {
return err
}

noteIdx, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("Invalid index for note: %s", args[0])
}
newNote := args[1]

now := time.Now()
currenctDayId := now.Format("20060102")
_, idx := journal.FetchEntryByID(currenctDayId, journalEntries)
if idx == -1 {
fmt.Println("Please run `workday start` first to create a new entry.")
return fmt.Errorf("Could not find any entry for the current day.")
}

if noteIdx < 0 || noteIdx >= len(journalEntries[idx].Notes) {
return fmt.Errorf("The index provided is not valid for the existing notes: %d", noteIdx)
}

journalEntries[idx].Notes[noteIdx] = newNote

err = journal.SaveEntries(journalEntries, journalPath)
if err != nil {
return err
}
fmt.Printf("Successfully edited note %d from the current day.\n", noteIdx)
return nil
}

func init() {
noteCmd.AddCommand(noteEditCmd)
}

0 comments on commit 3ced2c2

Please sign in to comment.