Skip to content

Commit

Permalink
feat(cmd/note): add note command
Browse files Browse the repository at this point in the history
feat(cmd/note): add basic note command function

this command will be used to add notes to the current workday entry

refactor(cmd/note): use cobra Args instead of conditionals

cobra provides a way to ensure a certain number of arguments are passed, we can use that instead of
checking it by hand with if statements

docs(cmd/note): add comments and description
  • Loading branch information
deadpyxel committed Oct 30, 2023
1 parent b2d48ec commit 782c91a
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions cmd/note.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"time"

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

// noteCmd represents the note command
var noteCmd = &cobra.Command{
Use: "note",
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("note called")
},
Use: "note [note]",
Args: cobra.ExactArgs(1),
Short: "Adds a note to the current workday entry",
Long: `The note command is used to add a note to the current workday entry.
It requires a single argument, which is the note to be added. The note must be provided as a double-quoted string.
If there is no entry for the current day, the command will print an error message and return an error.
Otherwise, it will add the note to the current entry and save the updated journal entries back to the file`,
RunE: addNoteToCurrentDay,
}

// addNoteToCurrentDay adds a note to the current workday entry.
// It first loads the existing journal entries from the file.
// If there is no entry for the current day, it prints an error message and returns an error.
// Otherwise, it adds the note to the current entry and saves the updated journal entries back to the file.
func addNoteToCurrentDay(cmd *cobra.Command, args []string) error {
journalEntries, err := journal.LoadEntries("journal.json")
if err != nil {
return err
}

newNote := args[0]

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.")
}
journalEntries[idx].Notes = append(journalEntries[idx].Notes, newNote)
fmt.Println("Successfully added new note to current day.")
return nil
}

func init() {
Expand Down

0 comments on commit 782c91a

Please sign in to comment.