Skip to content

Commit

Permalink
fixing logging, ensuring .minidoc folder exists
Browse files Browse the repository at this point in the history
  • Loading branch information
7onetella committed Feb 27, 2020
1 parent a7c8f5e commit e31b1da
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .minidoc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
json_logs: false
loglevel: debug
log_filename: .minidoc/minidoc.log
log_filename: minidoc.log
generated_doc_path: /Documents/
121 changes: 116 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package minidoc

import (
"bufio"
"encoding/json"
"fmt"
"github.com/7onetella/minidoc/config"
"github.com/google/uuid"
"github.com/mitchellh/go-homedir"
"os"
"strings"
)

Expand Down Expand Up @@ -76,13 +79,13 @@ func (s *Search) HandleCommand(command string) {

err = OpenFileIfNoneExist(markdownFilePath, markdown)
if err != nil {
s.App.StatusBar.SetText("[red]generating markdown: " + err.Error() + "[white]")
s.App.StatusBar.SetText("[red]generating content: " + err.Error() + "[white]")
return
}
OpenVim(s.App, markdownFilePath)
s.App.StatusBar.SetText("[yellow]markdown generated[white]")
s.App.StatusBar.SetText("[yellow]content generated[white]")

// convert markdown to pdf if the extension is pdf
// convert content to pdf if the extension is pdf
if extension == "pdf" {
// does pandoc exist in path?
if !DoesBinaryExists("pandoc") {
Expand All @@ -103,14 +106,14 @@ func (s *Search) HandleCommand(command string) {
return
}

// delete temporary markdown in /tmp folder
// delete temporary content in /tmp folder
DeleteFile(markdownFilePath)
return
}

err = Exec([]string{"open", markdownFilePath})
if err != nil {
s.App.StatusBar.SetText("[red]opening markdown: " + err.Error() + "[white]")
s.App.StatusBar.SetText("[red]opening content: " + err.Error() + "[white]")
return
}

Expand Down Expand Up @@ -194,6 +197,114 @@ func (s *Search) HandleCommand(command string) {

}
s.App.StatusBar.SetText("[white]untagged[white]")
case "export":
str := terms[1]

home, err := homedir.Dir()
if err != nil {
log.Errorf("finding home : %s", err)
}
tokens := strings.Split(str, ".")
if len(tokens) == 1 {
s.App.StatusBar.SetText("[red]please specify file extension[white]")
return
}

filename := tokens[0]
extension := tokens[1]

generatedDocPath := home + config.Config().GetString("generated_doc_path")

backupFilePath := generatedDocPath + filename + "." + extension

log.Debugf("exporting %s", backupFilePath)

content := ""
for i := 0; i < s.ResultList.GetRowCount(); i++ {
log.Debugf("current row %d", s.CurrentRowIndex)
doc, err := s.LoadMiniDocFromDB(i)
if err != nil {
log.Errorf("minidoc from failed: %v", err)
return
}

if !doc.IsSelected() {
log.Debugf("row %d not selected skipping", i)
continue
}

jsonBytes, err := json.Marshal(doc)
if err != nil {
log.Errorf("marshalling doc: %v", err)
return
}

content += string(jsonBytes) + "\n"
}

err = OpenFileIfNoneExist(backupFilePath, content)
if err != nil {
s.App.StatusBar.SetText("[red]exporting content: " + err.Error() + "[white]")
return
}
s.App.StatusBar.SetText("[yellow]exporting done[white]")

err = Exec([]string{"open", backupFilePath})
if err != nil {
s.App.StatusBar.SetText("[red]opening exported: " + err.Error() + "[white]")
return
}
case "import":
str := terms[1]

home, err := homedir.Dir()
if err != nil {
log.Errorf("finding home : %s", err)
}

generatedDocPath := home + config.Config().GetString("generated_doc_path")
tokens := strings.Split(str, ".")
if len(tokens) == 1 {
s.App.StatusBar.SetText("[red]please specify file extension[white]")
return
}

filename := tokens[0]
extension := tokens[1]

backupFilePath := generatedDocPath + filename + "." + extension

log.Debugf("importing %s", backupFilePath)

file, err := os.Open(backupFilePath)
if err != nil {
log.Errorf("opening: %v", err)
return
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)

var jsonMap interface{}
err = json.Unmarshal([]byte(line), &jsonMap)
if err != nil {
log.Errorf("unmarshaling json=%v", jsonMap)
return
}

doc, err := MiniDocFrom(jsonMap)
if err != nil {
log.Errorf("minidoc from doc=%v", doc)
return
}
// set the id to 0 so new sequence will be generated
doc.SetID(0)
s.App.DataHandler.Write(doc)
}
s.App.StatusBar.SetText("[yellow]importing done[white]")
}
}

Expand Down
15 changes: 12 additions & 3 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Logger interface {
}

var defaultLogger *logrus.Logger
var minidocHome string

func init() {
//defaultLogger = newLogrusLogger(config.Config())
Expand All @@ -48,7 +49,9 @@ func NewLogger(cfg config.Provider) *logrus.Logger {
return newLogrusLogger(cfg)
}

func GetNewLogrusLogger() *logrus.Logger {
func GetNewLogrusLogger(home string) *logrus.Logger {
minidocHome = home

if defaultLogger != nil {
return defaultLogger
}
Expand All @@ -74,9 +77,15 @@ func newLogrusLogger(cfg config.Provider) *logrus.Logger {
})

filename := cfg.GetString("log_filename")
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0755)
//pwd, err := os.Getwd()
//if err != nil {
// fmt.Println(err.Error())
//}
//fmt.Println("pwd: " + pwd)

f, err := os.OpenFile(minidocHome+"/"+filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
fmt.Println("error while opening log file to write " + err.Error())
fmt.Println("opening log file: " + err.Error())
}
l.SetOutput(f)
Logfile = f
Expand Down
2 changes: 1 addition & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *Search) Page() (title string, content tview.Primitive) {
return "Search", s.Layout
}

var words = []string{"@new", "@generate", "@tag", "@untag"}
var words = []string{"@new", "@generate", "@tag", "@untag", "@export", "@import"}

func (s *Search) InitSearchBar() {
//log.Debug("resetting search bar")
Expand Down
33 changes: 28 additions & 5 deletions simpleapp.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package minidoc

import (
"fmt"
l "github.com/7onetella/minidoc/log"
"github.com/gdamore/tcell"
"github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"

"github.com/rivo/tview"
"os"
"strconv"
)

var log = l.GetNewLogrusLogger()
func init() {
minidocHome := CreateMinidocHomeIfNotFound()
log = l.GetNewLogrusLogger(minidocHome)
}

var log *logrus.Logger

// SimpleApp comes with the menu and debug pane
type SimpleApp struct {
Expand Down Expand Up @@ -197,8 +206,6 @@ func (app *SimpleApp) Exit() {
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Quit" {
app.Stop()
l.Logfile.Sync()
l.Logfile.Close()
os.Exit(0)
} else {
if err := app.SetRoot(app.Layout, true).Run(); err != nil {
Expand All @@ -213,8 +220,6 @@ func (app *SimpleApp) Exit() {
}
}
app.Stop()
l.Logfile.Sync()
l.Logfile.Close()
os.Exit(0)
}

Expand Down Expand Up @@ -259,3 +264,21 @@ func newTextViewBar() *tview.TextView {
SetRegions(true).
SetWrap(false)
}

func CreateMinidocHomeIfNotFound() string {
homedir, _ := homedir.Dir() // return path with slash at the end
minidocHome := homedir + "/.minidoc"

if contains(os.Args, "--dev") {
pwd, err := os.Getwd()
if err != nil {
fmt.Println(err.Error())
}

minidocHome = pwd + "/.minidoc"
}

//fmt.Println("creating " + minidocHome)
os.MkdirAll(minidocHome, os.ModePerm)
return minidocHome
}

0 comments on commit e31b1da

Please sign in to comment.