From 3f4635d5d0ada6c8387a6955794d55cb9822bea1 Mon Sep 17 00:00:00 2001 From: TcM1911 Date: Fri, 30 Dec 2016 21:06:05 +0000 Subject: [PATCH] Add notebook optional restriction to delete cmd By using the -b or --notebook flag, the delete command will only search within the given notebook. --- cmd/deleteNote.go.go | 8 +++++++- evernote/note.go | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cmd/deleteNote.go.go b/cmd/deleteNote.go.go index 2878531..12ea3e7 100644 --- a/cmd/deleteNote.go.go +++ b/cmd/deleteNote.go.go @@ -34,10 +34,16 @@ To expunge the note you need to use the official client or the web client.`, fmt.Println("Error, a note title has to be given") return } - evernote.DeleteNote(args[0]) + nb, err := cmd.Flags().GetString("notebook") + if err != nil { + fmt.Println("Error when parsing the notebook name:", err) + return + } + evernote.DeleteNote(args[0], nb) }, } func init() { noteCmd.AddCommand(deleteNoteCmd) + deleteNoteCmd.Flags().StringP("notebook", "b", "", "The notebook of the note.") } diff --git a/evernote/note.go b/evernote/note.go index 00f5e7b..1ce1bbd 100644 --- a/evernote/note.go +++ b/evernote/note.go @@ -65,10 +65,21 @@ type Note struct { Notebook *Notebook } -// GetNote gets the note metadata from the server. -func GetNote(title string) *Note { +// GetNote gets the note metadata in the notebook from the server. +// If the notebook is an empty string, the first matching note will +// be returned. +func GetNote(title, notebook string) *Note { ns := user.GetNoteStore() filter := notestore.NewNoteFilter() + if notebook != "" { + nb, err := findNotebook(notebook) + if err != nil { + fmt.Println("Error when getting the notebook:", err) + os.Exit(1) + } + nbGUID := nb.GetGUID() + filter.NotebookGuid = &nbGUID + } filter.Words = &title notes, err := ns.FindNotes(user.AuthToken, filter, 0, 20) if err != nil { @@ -106,7 +117,7 @@ func convert(note *types.Note) *Note { // GetNoteWithContent returns the note with content from the user's notestore. func GetNoteWithContent(title string) *Note { - n := GetNote(title) + n := GetNote(title, "") ns := user.GetNoteStore() content, err := ns.GetNoteContent(user.AuthToken, n.GUID) if err != nil { @@ -124,13 +135,13 @@ func SaveChanges(n *Note) { } func ChangeTitle(old, new string) { - n := GetNote(old) + n := GetNote(old, "") n.Title = new saveChanges(n, false) } func MoveNote(noteTitle, notebookName string) { - n := GetNote(noteTitle) + n := GetNote(noteTitle, "") b, err := FindNotebook(notebookName) if err != nil { fmt.Println("Error when trying to retrieve notebook:", err) @@ -140,8 +151,8 @@ func MoveNote(noteTitle, notebookName string) { saveChanges(n, false) } -func DeleteNote(title string) { - n := GetNote(title) +func DeleteNote(title, notebook string) { + n := GetNote(title, notebook) ns := user.GetNoteStore() _, err := ns.DeleteNote(user.AuthToken, n.GUID) if err != nil {