diff --git a/README.md b/README.md index 9a86640a..2afde241 100644 --- a/README.md +++ b/README.md @@ -1,98 +1,144 @@ -# Dnote CLI - -![Dnote](assets/main.png) - -A command line interface for spontaneously capturing the things you learn while coding - -## Installation - -On macOS, or Linux, run: - - curl -s https://raw.githubusercontent.com/dnote-io/cli/master/install.sh | sh - -In some cases, you might need `sudo`. Feel free to inspect [install.sh](https://github.com/dnote-io/cli/blob/master/install.sh): - - curl -s https://raw.githubusercontent.com/dnote-io/cli/master/install.sh | sudo sh - -On Windows, download [binary](https://github.com/dnote-io/cli/releases) - -## Overview - -Dnote categorizes your **notes** by **books**. - -All your books and notes are stored in `$HOME/.dnote` as a JSON file. - -You can optionally sync your note with Dnote server. Syncing will allow you to interact with your notes using the web frontend at https://dnote.io, and set up digest notifications. - -## Commands - -### dnote use [book name] -*alias: u* - -Change the book to write your note in. - -e.g. - - dnote use linux - -### dnote new "[note]" -*alias: n* - -Write a new note under the current book. - -e.g. - - dnote new "set -e instructs bash to exit immediately if any command has non-zero exit status" - -### dnote books -*alias: b* - -List all the books that you created - -e.g. - - $ dnote books - javascript - * linux - tmux - css - -### dnote notes - -List all notes in the current book - -#### Options - -* `-b [book name]` - -Specify the name of the book to read from - -e.g. - - $ dnote notes - On note JS - * .bind() creates a new function - * arrow function uses less memory than function with .bind() - * the time passed to setTimeout is minimum, no guaranteed time - -### dnote sync - -Sync notes with Dnote server - -### dnote login - -Start a login procedure which will store the APIKey to communicate with the server - - -## Links - -* [Website](https://dnote.io) -* [Making Dnote (blog article)](https://sungwoncho.io/making-dnote/) - -## License - -MIT - -------- - -> Made by [sung](https://sungwoncho.io) +# Dnote CLI + +![Dnote](assets/main.png) + +A command line interface for spontaneously capturing the things you learn while coding + +## Installation + +On macOS, or Linux, run: + + curl -s https://raw.githubusercontent.com/dnote-io/cli/master/install.sh | sh + +In some cases, you might need `sudo`. Feel free to inspect [install.sh](https://github.com/dnote-io/cli/blob/master/install.sh): + + curl -s https://raw.githubusercontent.com/dnote-io/cli/master/install.sh | sudo sh + +On Windows, download [binary](https://github.com/dnote-io/cli/releases) + +## Overview + +Dnote categorizes your **notes** by **books**. + +All your books and notes are stored in `$HOME/.dnote` as a JSON file. + +You can optionally sync your note with Dnote server. Syncing will allow you to interact with your notes using the web frontend at https://dnote.io, and set up digest notifications. + +## Commands + +### dnote use "[book name]" +*alias: u* + +Change the book to write your note in. + +e.g. + + dnote use linux + +### dnote new "[note]" +*alias: n* + +Write a new note under the current book. + +e.g. + + dnote new "set -e instructs bash to exit immediately if any command has non-zero exit status" + +### dnote edit "[note index]" "[content]" +*alias: e* + +Overwrite a note under the current book + +#### Option +*`-b [book name]` + +Specify the name of the book to edit from + +e.g + + $ dnote notes + * [0] - Content index 0. + * [1] - Content index 1. + * [2] - Content index 2. + + $ dnote edit 1 "New content" + [+] Edited Note : 1 + + $ dnote notes + * [0] - Content index 0. + * [1] - New content. + * [2] - Content index 2. + +### dnote delete "[note]" +*alias: d* + +Delete a note under the current book + +#### Option +*`-n [note index]` + +Specify the index of the note to be deleted + +*`-b [book name]` + +Specify the name of the book to be deleted from + +*`--book [book name]` + +e.g. + + $ dnote delete -n [note index] + $ dnote delete -b [book name] [note index] + + +### dnote books +*alias: b* + +List all the books that you created + +e.g. + + $ dnote books + javascript + * linux + tmux + css + +### dnote notes + +List all notes in the current book + +#### Options + +* `-b [book name]` + +Specify the name of the book to read from + +e.g. + + $ dnote notes + On note JS + * .bind() creates a new function + * arrow function uses less memory than function with .bind() + * the time passed to setTimeout is minimum, no guaranteed time + +### dnote sync + +Sync notes with Dnote server + +### dnote login + +Start a login procedure which will store the APIKey to communicate with the server + +## Links + +* [Website](https://dnote.io) +* [Making Dnote (blog article)](https://sungwoncho.io/making-dnote/) + +## License + +MIT + +------- + +> Made by [sung](https://sungwoncho.io) diff --git a/cmd/delete/delete.go b/cmd/delete/delete.go new file mode 100644 index 00000000..1e12f1b1 --- /dev/null +++ b/cmd/delete/delete.go @@ -0,0 +1,97 @@ +package delete + +import ( + "fmt" + "os" + "strconv" + + "github.com/dnote-io/cli/utils" +) + +// Delete is a facade for deleting either note or book +func Delete() error { + if os.Args[2] == "-b" { + book(os.Args[3]) + } else if len(os.Args) == 4 { + targetBook := os.Args[2] + noteIndex, err := strconv.Atoi(os.Args[3]) + if err != nil { + return err + } + + note(noteIndex, targetBook) + } else { + fmt.Println("Error : Invalid argument passed to delete.") + } + + return nil +} + +// note deletes the note in a certain index. +func note(index int, book string) error { + ok, err := utils.AskConfirmation("Are you sure?") + if err != nil { + return err + } + if !ok { + return nil + } + + dnote, err := utils.GetDnote() + if err != nil { + return err + } + + if len(dnote[book])-1 < index { + fmt.Println("Error : The note with that index is not found.") + return nil + } + + content := dnote[book][index].Content + dnote[book] = append(dnote[book][:index], dnote[book][index+1:]...) + err = utils.WriteDnote(dnote) + if err != nil { + return err + } + + fmt.Printf("[-] Deleted : %d | Content : %s\n", index, content) + return nil + +} + +// book deletes a book with the given name +func book(bookName string) error { + ok, err := utils.AskConfirmation("Are you sure?") + if err != nil { + return err + } + if !ok { + return nil + } + + dnote, err := utils.GetDnote() + if err != nil { + return err + } + + books, err := utils.GetBooks() + if err != nil { + return err + } + + for _, book := range books { + if book == bookName { + delete(dnote, bookName) + err := utils.WriteDnote(dnote) + if err != nil { + return err + } + + fmt.Printf("[-] Deleted book : %s \n", bookName) + return nil + } + } + + fmt.Println("Error : The book with that name is not found.") + return nil +} diff --git a/cmd/edit/edit.go b/cmd/edit/edit.go new file mode 100644 index 00000000..da72b11a --- /dev/null +++ b/cmd/edit/edit.go @@ -0,0 +1,54 @@ +package edit + +import ( + "fmt" + "os" + "strconv" + + "github.com/dnote-io/cli/utils" +) + +func Edit() error { + dnote, err := utils.GetDnote() + if err != nil { + return err + } + + var target_book string + var index int + var content string + + if len(os.Args) == 4 { + target_book, err = utils.GetCurrentBook() + if err != nil { + return err + } + index, err = strconv.Atoi(os.Args[2]) + if err != nil { + return err + } + content = os.Args[3] + }else if len(os.Args) == 5 { + target_book = os.Args[2] + index, err = strconv.Atoi(os.Args[3]) + if err != nil { + return err + } + content = os.Args[4] + } + + for i, note := range dnote[target_book] { + if i == index { + note.Content = content + dnote[target_book][i] = note + + err := utils.WriteDnote(dnote) + fmt.Printf("[+] Edited Note : %d \n", index) + return err + } + } + + // If loop finishes without returning, note did not exist + fmt.Println("Error : The note with that index is not found.") + return nil +} diff --git a/cmd/notes/notes.go b/cmd/notes/notes.go index 358681dd..05db830a 100644 --- a/cmd/notes/notes.go +++ b/cmd/notes/notes.go @@ -33,8 +33,8 @@ func Run() error { for k, v := range dnote { if k == bookName { - for _, note := range v { - fmt.Printf("* %s\n", note.Content) + for i, note := range v { + fmt.Printf("* [%d] - %s\n", i, note.Content) } } } diff --git a/main.go b/main.go index f5a254dc..7a3fd428 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,9 @@ import ( "github.com/dnote-io/cli/cmd/sync" "github.com/dnote-io/cli/upgrade" "github.com/dnote-io/cli/utils" + + "github.com/dnote-io/cli/cmd/delete" + "github.com/dnote-io/cli/cmd/edit" ) func initDnote() error { @@ -130,6 +133,12 @@ func main() { note := os.Args[2] err := new.Run(note) check(err) + case "edit", "e": + err := edit.Edit() + check(err) + case "delete", "d": + err := delete.Delete() + check(err) case "books", "b": err := books.Run() check(err)