Skip to content

Commit

Permalink
Merge branch 'feat/refacto-db'
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasnevespereira committed Nov 20, 2023
2 parents 0a32a58 + 62648d1 commit 8458a39
Show file tree
Hide file tree
Showing 17 changed files with 387 additions and 172 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ lembra.log
lembra.pid
coverage.out
dist/
remindersdb
reminders.json
6 changes: 1 addition & 5 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
project_name: lembra
builds:
- binary: lembra
ldflags:
- '-s -w -extldflags "-static"'
env:
- CGO_ENABLED=1
- CGO_CFLAGS=-static
- CGO_LDFLAGS=-static
- CGO_ENABLED=0
goos:
- linux
- windows
Expand Down
10 changes: 4 additions & 6 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cmd

import (
"fmt"
"github.com/lucasnevespereira/lembra/internal/pkg/reminder"
"github.com/lucasnevespereira/lembra/internal/pkg/repository"
"github.com/lucasnevespereira/lembra/internal/pkg/repository/database"
"github.com/lucasnevespereira/lembra/internal/pkg/storage"
"github.com/lucasnevespereira/lembra/internal/utils/mapping"
_ "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,11 +31,11 @@ func createReminder(cmd *cobra.Command, args []string) error {
message, _ := cmd.Flags().GetString("message")
timeStr, _ := cmd.Flags().GetString("time")

db, err := database.Open()
dbFile, err := storage.OpenStorageFile()
if err != nil {
return fmt.Errorf("open db connection: %v\n", err)
return err
}
reminderRepo := repository.NewReminderRepository(db)
reminderRepo := storage.NewReminderStorage(dbFile)

time, err := reminder.ParseTime(timeStr)
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package cmd

import (
"fmt"
"github.com/lucasnevespereira/lembra/internal/pkg/repository"
"github.com/lucasnevespereira/lembra/internal/pkg/repository/database"
"github.com/lucasnevespereira/lembra/internal/pkg/storage"
"github.com/lucasnevespereira/lembra/internal/utils/logger"
"github.com/spf13/cobra"
)
Expand All @@ -25,14 +24,14 @@ func deleteReminder(cmd *cobra.Command, args []string) error {
id, _ := cmd.Flags().GetString("id")
deleteAll, _ := cmd.Flags().GetBool("all")

db, err := database.Open()
dbFile, err := storage.OpenStorageFile()
if err != nil {
return fmt.Errorf("open db connection: %v\n", err)
return err
}
reminderRepo := repository.NewReminderRepository(db)
reminderRepo := storage.NewReminderStorage(dbFile)

if deleteAll {
err = reminderRepo.DeleteAll()
err := reminderRepo.DeleteAll()
if err != nil {
return err
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cmd

import (
"fmt"
"github.com/lucasnevespereira/lembra/internal/pkg/repository"
"github.com/lucasnevespereira/lembra/internal/pkg/repository/database"
"github.com/lucasnevespereira/lembra/internal/pkg/storage"
"github.com/lucasnevespereira/lembra/internal/utils/mapping"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand All @@ -23,11 +21,11 @@ func init() {

func listReminders(cmd *cobra.Command, args []string) error {

db, err := database.Open()
dbFile, err := storage.OpenStorageFile()
if err != nil {
return fmt.Errorf("open db connection: %v\n", err)
return err
}
reminderRepo := repository.NewReminderRepository(db)
reminderRepo := storage.NewReminderStorage(dbFile)

dbReminders, err := reminderRepo.GetAll()
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package cmd

import (
"github.com/lucasnevespereira/lembra/internal/pkg/notifier"
"github.com/lucasnevespereira/lembra/internal/pkg/repository"
"github.com/lucasnevespereira/lembra/internal/pkg/repository/database"
"github.com/lucasnevespereira/lembra/internal/pkg/storage"
"github.com/lucasnevespereira/lembra/internal/utils/logger"
"github.com/robfig/cron"
"github.com/sevlyar/go-daemon"
Expand Down Expand Up @@ -43,16 +42,16 @@ func runDaemon(cmd *cobra.Command, args []string) {
defer func(daemonContext *daemon.Context) {
err := daemonContext.Release()
if err != nil {
logger.Log.Errorf("failed to release daemon ressources: %v", err)
logger.Log.Errorf("failed to release daemon resources: %v", err)
}
}(daemonContext)

db, err := database.Open()
// Perform DB operations
dbFile, err := storage.OpenStorageFile()
if err != nil {
logger.Log.Errorf("open db connection: %v\n", err)
logger.Log.Fatalf("open storage file: %v", err)
}
reminderRepo := repository.NewReminderRepository(db)

reminderRepo := storage.NewReminderStorage(dbFile)
notifier := notifier.NewCronNotifier(reminderRepo, cron.New())
if err := notifier.Start(); err != nil {
logger.Log.Fatalf("starting notifier: %v", err)
Expand Down
9 changes: 4 additions & 5 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package cmd
import (
"fmt"
"github.com/lucasnevespereira/lembra/internal/pkg/reminder"
"github.com/lucasnevespereira/lembra/internal/pkg/repository"
"github.com/lucasnevespereira/lembra/internal/pkg/repository/database"
"github.com/lucasnevespereira/lembra/internal/pkg/storage"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -32,11 +31,11 @@ func updateReminder(cmd *cobra.Command, args []string) error {
message, _ := cmd.Flags().GetString("message")
timeStr, _ := cmd.Flags().GetString("time")

db, err := database.Open()
dbFile, err := storage.OpenStorageFile()
if err != nil {
return fmt.Errorf("open db connection: %v\n", err)
return err
}
reminderRepo := repository.NewReminderRepository(db)
reminderRepo := storage.NewReminderStorage(dbFile)

existingReminder, err := reminderRepo.GetByID(id)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,33 @@ require (
)

require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/badger/v3 v3.2103.5 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
go.opencensus.io v0.22.5 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/sys v0.8.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 8458a39

Please sign in to comment.