Add debug statements (#109)

* Add debug

* Document usage
This commit is contained in:
Sung Won Cho 2018-09-08 16:21:55 +10:00 committed by GitHub
commit 49754b58a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 1 deletions

View file

@ -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

View file

@ -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.

View file

@ -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")

View file

@ -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...))
}
}