From 3ced2c299cd17157d478aecb57d622fe3fdf3168 Mon Sep 17 00:00:00 2001 From: Robson Cruz Date: Fri, 17 Nov 2023 16:07:08 -0300 Subject: [PATCH] feat(cmd/note): add note edit command --- cmd/note.go | 10 -------- cmd/note_edit.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 cmd/note_edit.go diff --git a/cmd/note.go b/cmd/note.go index 6ee08c9..70bd628 100644 --- a/cmd/note.go +++ b/cmd/note.go @@ -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") } diff --git a/cmd/note_edit.go b/cmd/note_edit.go new file mode 100644 index 0000000..b3c716b --- /dev/null +++ b/cmd/note_edit.go @@ -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) +}