Skip to content

Commit

Permalink
Add -v flag to turn on verbose output with debug information
Browse files Browse the repository at this point in the history
  • Loading branch information
wormi4ok committed Sep 16, 2020
1 parent 7e03c5a commit 3738fd8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
github.com/cheggaaa/pb/v3 v3.0.4
github.com/fatih/color v1.9.0 // indirect
github.com/hashicorp/logutils v1.0.0
github.com/integrii/flaggy v1.4.4
github.com/mattn/godown v0.0.0-20200217152941-afc959f6a561
golang.org/x/exp v0.0.0-20200915172826-20d5ce0eab31
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/integrii/flaggy v1.4.4 h1:8fGyiC14o0kxhTqm2VBoN19fDKPZsKipP7yggreTMDc=
github.com/integrii/flaggy v1.4.4/go.mod h1:tnTxHeTJbah0gQ6/K0RW0J7fMUBk9MCF5blhm43LNpI=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
Expand Down
46 changes: 37 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"

"github.com/cheggaaa/pb/v3"
"github.com/hashicorp/logutils"
"github.com/integrii/flaggy"

"github.com/wormi4ok/evernote2md/encoding/enex"
Expand All @@ -29,7 +30,7 @@ func main() {
var input string
var outputDir = filepath.FromSlash("./notes")
var outputOverride string
var folders, noHighlights bool
var folders, noHighlights, debug bool

flaggy.SetName("evernote2md")
flaggy.SetDescription(" Convert Evernote notes exported in *.enex format to markdown files")
Expand All @@ -41,6 +42,7 @@ func main() {

flaggy.Bool(&folders, "", "folders", "Put every note in a separate folder")
flaggy.Bool(&noHighlights, "", "noHighlights", "Disable converting evernote highlights to inline HTML tags")
flaggy.Bool(&debug, "v", "debug", "Show debug output")

flaggy.DefaultParser.ShowHelpOnUnexpected = false
flaggy.DefaultParser.AdditionalHelpPrepend = "http://github.com/wormi4ok/evernote2md"
Expand All @@ -51,15 +53,15 @@ func main() {
outputDir = outputOverride
}

run(input, outputDir, folders, !noHighlights)
}
setLogLevel(debug)

const progressBarTmpl = `Notes: {{counters .}} {{bar . "[" "=" ">" "_" "]" }} {{percent .}} {{etime .}}`
run(input, outputDir, newProgressBar(debug), folders, !noHighlights)
}

// A map to keep track of what notes are already created
var notes = map[string]int{}

func run(input, output string, folders, highlights bool) {
func run(input, output string, progress *pb.ProgressBar, folders, highlights bool) {
i, err := os.Open(input)
failWhen(err)

Expand All @@ -72,9 +74,8 @@ func run(input, output string, folders, highlights bool) {
err = os.MkdirAll(output, os.ModePerm)
failWhen(err)

progress := pb.StartNew(len(export.Notes))
progress.SetTemplateString(progressBarTmpl)

progress.SetTotal(int64(len(export.Notes)))
progress.Start()
c := internal.Converter{EnableHighlights: highlights}
n := export.Notes
for i := range n {
Expand All @@ -96,11 +97,13 @@ func run(input, output string, folders, highlights bool) {

// saveNote along with media resources
func saveNote(path string, title string, md *markdown.Note) error {
log.Printf("[DEBUG] Saving file %s/%s", path, title)
if err := file.Save(path, title, bytes.NewReader(md.Content)); err != nil {
return fmt.Errorf("save file %s: %w", path+"/"+title, err)
}
for _, res := range md.Media {
mediaPath := path + "/" + string(res.Type)
log.Printf("[DEBUG] Saving attachment %s/%s", mediaPath, res.Name)
if err := file.Save(mediaPath, res.Name, bytes.NewReader(res.Content)); err != nil {
return fmt.Errorf("save resource %s: %w", mediaPath+"/"+res.Name, err)
}
Expand All @@ -122,8 +125,33 @@ func uniqueName(title string) string {
return name
}

const progressBarTmpl = `Notes: {{counters .}} {{bar . "[" "=" ">" "_" "]" }} {{percent .}} {{etime .}}`

func newProgressBar(debug bool) *pb.ProgressBar {
progress := new(pb.ProgressBar)
progress.SetTemplateString(progressBarTmpl)
if debug {
progress.SetWriter(new(bytes.Buffer))
}
return progress
}

func setLogLevel(debug bool) {
var logLevel logutils.LogLevel = "WARN"

if debug {
logLevel = "DEBUG"
}

log.SetOutput(&logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "WARN", "ERROR"},
MinLevel: logLevel,
Writer: os.Stderr,
})
}

func failWhen(err error) {
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("[ERROR] %w", err))
}
}

0 comments on commit 3738fd8

Please sign in to comment.