Skip to content

Commit

Permalink
Remove dependency on resty and refer to logrus by name
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Crane <[email protected]>
  • Loading branch information
marcus-crane committed May 28, 2023
1 parent 3892e23 commit 537cff7
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 84 deletions.
9 changes: 5 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package main

import (
"context"

"github.com/marcus-crane/october/backend"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

// App struct
Expand All @@ -25,11 +26,11 @@ func (a *App) startup(ctx context.Context) {
func (a *App) domReady(ctx context.Context) {
a.ctx = ctx
backend.StartLogger()
log.WithContext(ctx).Info("Logger should be initialised now")
log.WithContext(ctx).Info("Backend is about to start up")
logrus.WithContext(ctx).Info("Logger should be initialised now")
logrus.WithContext(ctx).Info("Backend is about to start up")
}

func (a *App) shutdown(ctx context.Context) {
log.WithContext(ctx).Info("Shutting down. Goodbye!")
logrus.WithContext(ctx).Info("Shutting down. Goodbye!")
backend.CloseLogFile()
}
4 changes: 2 additions & 2 deletions backend/db.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package backend

import (
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
Expand All @@ -11,7 +11,7 @@ var Conn *gorm.DB
func OpenConnection(filepath string) error {
conn, err := gorm.Open(sqlite.Open(filepath), &gorm.Config{})
if err != nil {
log.WithError(err).WithField("filepath", filepath).Error("Failed to open DB connection")
logrus.WithError(err).WithField("filepath", filepath).Error("Failed to open DB connection")
return err
}
Conn = conn
Expand Down
35 changes: 18 additions & 17 deletions backend/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package backend

import (
"fmt"
log "github.com/sirupsen/logrus"

"github.com/sirupsen/logrus"

"github.com/pgaskin/koboutils/v2/kobo"
)
Expand Down Expand Up @@ -152,12 +153,12 @@ func GetKoboMetadata(detectedPaths []string) []Kobo {
for _, path := range detectedPaths {
_, _, deviceId, err := kobo.ParseKoboVersion(path)
if err != nil {
log.WithField("kobo_path", path).WithError(err).Error("Failed to parse Kobo version")
logrus.WithField("kobo_path", path).WithError(err).Error("Failed to parse Kobo version")
}
log.WithField("device_id", deviceId).Info("Found an attached device")
logrus.WithField("device_id", deviceId).Info("Found an attached device")
device, found := kobo.DeviceByID(deviceId)
if !found {
log.WithField("device_id", deviceId).Warn("Found a device that isn't officially supported but will likely still operate just fine")
logrus.WithField("device_id", deviceId).Warn("Found a device that isn't officially supported but will likely still operate just fine")
// We can handle unsupported Kobos in future but at present, there are none
kobos = append(kobos, Kobo{
MntPath: path,
Expand Down Expand Up @@ -193,65 +194,65 @@ func (k *Kobo) ListBookmarksByID(contentID string) ([]Bookmark, error) {
&Bookmark{VolumeID: contentID},
).Where("VolumeID LIKE '%file:///%'").Find(&bookmark)
if result.Error != nil {
log.WithError(result.Error).WithField("content_id", contentID).Error("Encountered an error while trying to list bookmarks by ID")
logrus.WithError(result.Error).WithField("content_id", contentID).Error("Encountered an error while trying to list bookmarks by ID")
return nil, result.Error
}
return bookmark, nil
}

func (k *Kobo) FindBookOnDevice(bookID string) (Content, error) {
var content Content
log.WithField("book_id", bookID).Debug("Retrieving a book that has been uploaded to Readwise previously")
logrus.WithField("book_id", bookID).Debug("Retrieving a book that has been uploaded to Readwise previously")
result := Conn.Where(&Content{ContentType: "6", VolumeIndex: -1, ContentID: bookID}).Where("VolumeID LIKE '%file:///%'").Find(&content)
if result.Error != nil {
log.WithError(result.Error).WithField("book_id", bookID).Error("Failed to retrieve content from device")
logrus.WithError(result.Error).WithField("book_id", bookID).Error("Failed to retrieve content from device")
return content, result.Error
}
log.WithField("title", content.Title).Debug("Successfully retrieved content from device DB")
logrus.WithField("title", content.Title).Debug("Successfully retrieved content from device DB")
return content, nil
}

func (k *Kobo) ListDeviceContent() ([]Content, error) {
var content []Content
log.Debug("Retrieving content list from device")
logrus.Debug("Retrieving content list from device")
result := Conn.Where(
&Content{ContentType: "6", VolumeIndex: -1},
).Where("ContentID LIKE '%file:///%'").Order("___PercentRead desc, title asc").Find(&content)
if result.Error != nil {
log.WithError(result.Error).Error("Failed to retrieve content from device")
logrus.WithError(result.Error).Error("Failed to retrieve content from device")
return nil, result.Error
}
log.WithField("content_count", len(content)).Debug("Successfully retrieved device content")
logrus.WithField("content_count", len(content)).Debug("Successfully retrieved device content")
return content, nil
}

func (k *Kobo) ListDeviceBookmarks() ([]Bookmark, error) {
var bookmarks []Bookmark
log.Debug("Retrieving bookmarks from device")
logrus.Debug("Retrieving bookmarks from device")
result := Conn.Where("VolumeID LIKE '%file:///%'").Order("VolumeID ASC, ChapterProgress ASC").Find(&bookmarks).Limit(1)
if result.Error != nil {
log.WithError(result.Error).Error("Failed to retrieve bookmarks from device")
logrus.WithError(result.Error).Error("Failed to retrieve bookmarks from device")
return nil, result.Error
}
log.WithField("bookmark_count", len(bookmarks)).Debug("Successfully retrieved device bookmarks")
logrus.WithField("bookmark_count", len(bookmarks)).Debug("Successfully retrieved device bookmarks")
return bookmarks, nil
}

func (k *Kobo) BuildContentIndex(content []Content) map[string]Content {
log.Debug("Building an index out of device content")
logrus.Debug("Building an index out of device content")
contentIndex := make(map[string]Content)
for _, item := range content {
contentIndex[item.ContentID] = item
}
log.WithField("index_count", len(contentIndex)).Debug("Built content index")
logrus.WithField("index_count", len(contentIndex)).Debug("Built content index")
return contentIndex
}

func (k *Kobo) CountDeviceBookmarks() int64 {
var count int64
result := Conn.Model(&Bookmark{}).Count(&count)
if result.Error != nil {
log.WithError(result.Error).Error("Failed to count bookmarks on device")
logrus.WithError(result.Error).Error("Failed to count bookmarks on device")
}
return count
}
6 changes: 3 additions & 3 deletions backend/device_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package backend

import (
"log"
"os"
"path/filepath"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

Expand All @@ -20,12 +20,12 @@ func setupTmpKobo(dir string, deviceId string) string {
content := []byte(deviceId)
err := os.Mkdir(filepath.Join(dir, ".kobo"), 0777)
if err != nil {
log.Fatal(err)
logrus.Fatal(err)
return ""
}
tmpfn := filepath.Join(dir, ".kobo", "version")
if err := os.WriteFile(tmpfn, content, 0666); err != nil {
log.Fatal(err)
logrus.Fatal(err)
return ""
}
return dir
Expand Down
14 changes: 7 additions & 7 deletions backend/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"strings"

"github.com/adrg/xdg"
"github.com/sirupsen/logrus"

"github.com/pgaskin/koboutils/v2/kobo"
log "github.com/sirupsen/logrus"

wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
Expand All @@ -35,7 +35,7 @@ type Backend struct {
func StartBackend(ctx *context.Context, version string) *Backend {
settings, err := LoadSettings()
if err != nil {
log.WithContext(*ctx).WithError(err).Error("Failed to load settings")
logrus.WithContext(*ctx).WithError(err).Error("Failed to load settings")
}
return &Backend{
SelectedKobo: Kobo{},
Expand Down Expand Up @@ -83,7 +83,7 @@ func (b *Backend) NavigateExplorerToLogLocation() {
}
logLocation, err := xdg.DataFile("october/logs")
if err != nil {
log.WithError(err).Error("Failed to determine XDG data location for opening log location in explorer")
logrus.WithError(err).Error("Failed to determine XDG data location for opening log location in explorer")
}
// We will always get an error because the file explorer doesn't exit so it is unable to
// return a 0 successful exit code until y'know, the user exits the window
Expand All @@ -92,7 +92,7 @@ func (b *Backend) NavigateExplorerToLogLocation() {

func (b *Backend) DetectKobos() []Kobo {
connectedKobos, err := kobo.Find()
log.WithField("kobos_found", len(connectedKobos)).Info("Detected one or more Kobos")
logrus.WithField("kobos_found", len(connectedKobos)).Info("Detected one or more Kobos")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (b *Backend) ForwardToReadwise() (int, error) {
absCoverPath := path.Join(b.SelectedKobo.MntPath, "/", coverPath)
coverBytes, err := os.ReadFile(absCoverPath)
if err != nil {
log.WithError(err).WithFields(log.Fields{"cover": book.SourceURL, "location": absCoverPath}).Warn("Failed to load cover. Carrying on")
logrus.WithError(err).WithFields(logrus.Fields{"cover": book.SourceURL, "location": absCoverPath}).Warn("Failed to load cover. Carrying on")
}
var base64Encoding string
mimeType := http.DetectContentType(coverBytes)
Expand All @@ -189,9 +189,9 @@ func (b *Backend) ForwardToReadwise() (int, error) {
base64Encoding += base64.StdEncoding.EncodeToString(coverBytes)
err = b.Readwise.UploadCover(base64Encoding, book.ID, b.Settings.ReadwiseToken)
if err != nil {
log.WithError(err).WithField("cover", book.SourceURL).Error("Failed to upload cover to Readwise")
logrus.WithError(err).WithField("cover", book.SourceURL).Error("Failed to upload cover to Readwise")
}
log.WithField("cover", book.SourceURL).Debug("Successfully uploaded cover to Readwise")
logrus.WithField("cover", book.SourceURL).Debug("Successfully uploaded cover to Readwise")
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions backend/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/adrg/xdg"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

var logFileHandle *os.File
Expand All @@ -18,14 +18,14 @@ func StartLogger() {
if err != nil {
panic("Failed to create location to store logfiles")
}
log.SetFormatter(&log.JSONFormatter{})
logrus.SetFormatter(&logrus.JSONFormatter{})
file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
logFileHandle = file
log.SetOutput(file)
logrus.SetOutput(file)
} else {
log.WithError(err).Error(err)
log.Error("Failed to create log file, using stdout")
logrus.WithError(err).Error(err)
logrus.Error("Failed to create log file, using stdout")
}
}

Expand Down
Loading

0 comments on commit 537cff7

Please sign in to comment.