From 49754b58a407024f5280ce1f132ad61bf5d8eb56 Mon Sep 17 00:00:00 2001 From: Sung Won Cho Date: Sat, 8 Sep 2018 16:21:55 +1000 Subject: [PATCH] Add debug statements (#109) * Add debug * Document usage --- COMMANDS.md | 2 +- CONBTRIBUTING.md | 4 ++++ core/reducer.go | 13 +++++++++++++ log/log.go | 8 ++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/COMMANDS.md b/COMMANDS.md index 5552f09e..e7e5e254 100644 --- a/COMMANDS.md +++ b/COMMANDS.md @@ -50,7 +50,7 @@ Edit a note $ dnote edit linux 1 # Edit a note with the given index in the specified book with a content. -$ dnote edit linux 1 "New Content" +$ dnote edit linux 1 -c "New Content" ``` ## dnote remove diff --git a/CONBTRIBUTING.md b/CONBTRIBUTING.md index 771e8081..673ef523 100644 --- a/CONBTRIBUTING.md +++ b/CONBTRIBUTING.md @@ -24,6 +24,10 @@ Run ./scripts/test.sh ``` +## Debug + +Run Dnote with `DNOTE_DEBUG=1` to print debugging statements. + ## Release This project uses [goreleaser](https://github.com/goreleaser/goreleaser) to automate the release process. diff --git a/core/reducer.go b/core/reducer.go index 70f4ab42..ec28f296 100644 --- a/core/reducer.go +++ b/core/reducer.go @@ -6,6 +6,7 @@ import ( "github.com/dnote/actions" "github.com/dnote/cli/infra" + "github.com/dnote/cli/log" "github.com/pkg/errors" ) @@ -54,6 +55,8 @@ func handleAddNote(ctx infra.DnoteCtx, action actions.Action) error { return errors.Wrap(err, "Failed to parse the action data") } + log.Debug("reducing add_note. action: %+v. data: %+v\n", action, data) + note := infra.Note{ UUID: data.NoteUUID, Content: data.Content, @@ -99,6 +102,8 @@ func handleRemoveNote(ctx infra.DnoteCtx, action actions.Action) error { return errors.Wrap(err, "Failed to parse the action data") } + log.Debug("reducing remove_note. action: %+v. data: %+v\n", action, data) + dnote, err := GetDnote(ctx) if err != nil { return errors.Wrap(err, "Failed to get dnote") @@ -128,6 +133,8 @@ func handleEditNoteV1(ctx infra.DnoteCtx, action actions.Action) error { return errors.Wrap(err, "Failed to parse the action data") } + log.Debug("reducing edit_note v1. action: %+v. data: %+v\n", action, data) + dnote, err := GetDnote(ctx) if err != nil { return errors.Wrap(err, "Failed to get dnote") @@ -187,6 +194,8 @@ func handleEditNoteV2(ctx infra.DnoteCtx, action actions.Action) error { return errors.Wrap(err, "Failed to parse the action data") } + log.Debug("reducing edit_note v2. action: %+v. data: %+v\n", action, data) + dnote, err := GetDnote(ctx) if err != nil { return errors.Wrap(err, "Failed to get dnote") @@ -269,6 +278,8 @@ func handleAddBook(ctx infra.DnoteCtx, action actions.Action) error { return errors.Wrap(err, "Failed to parse the action data") } + log.Debug("reducing add_book. action: %+v. data: %+v\n", action, data) + dnote, err := GetDnote(ctx) if err != nil { return errors.Wrap(err, "Failed to get dnote") @@ -302,6 +313,8 @@ func handleRemoveBook(ctx infra.DnoteCtx, action actions.Action) error { return errors.Wrap(err, "Failed to parse the action data") } + log.Debug("reducing remove_book. action: %+v. data: %+v\n", action, data) + dnote, err := GetDnote(ctx) if err != nil { return errors.Wrap(err, "Failed to get dnote") diff --git a/log/log.go b/log/log.go index 8451149b..72cedada 100644 --- a/log/log.go +++ b/log/log.go @@ -2,6 +2,7 @@ package log import ( "fmt" + "os" "github.com/fatih/color" ) @@ -55,3 +56,10 @@ func Errorf(msg string, v ...interface{}) { func Printf(msg string, v ...interface{}) { fmt.Fprintf(color.Output, "%s%s %s", indent, SprintfGray("•"), fmt.Sprintf(msg, v...)) } + +// Debug prints to the console if DNOTE_DEBUG is set +func Debug(msg string, v ...interface{}) { + if os.Getenv("DNOTE_DEBUG") == "1" { + fmt.Fprintf(color.Output, "%s %s", SprintfGray("DEBUG:"), fmt.Sprintf(msg, v...)) + } +}