Skip to content

Commit

Permalink
feat: separate repo concerns + logger + tablewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasnevespereira committed May 23, 2023
1 parent 684ddc8 commit 4b88c0f
Show file tree
Hide file tree
Showing 19 changed files with 291 additions and 141 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ lembra
bin/
reminders.db
lembra.log
lembra.pid
lembra.pid
coverage.out
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ update:
delete:
bin/$(APP_NAME) delete --id='$(id)'

delete-all:
bin/$(APP_NAME) delete --all=true

list:
bin/$(APP_NAME) list

Expand Down
10 changes: 7 additions & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
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/utils/mapping"
_ "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -30,10 +33,11 @@ func createReminder(cmd *cobra.Command, args []string) error {
message, _ := cmd.Flags().GetString("message")
timeStr, _ := cmd.Flags().GetString("time")

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

time, err := reminder.ParseTime(timeStr)
if err != nil {
Expand All @@ -44,7 +48,7 @@ func createReminder(cmd *cobra.Command, args []string) error {
return err
}

err = reminderRepo.Create(newReminder)
err = reminderRepo.Create(mapping.ToReminderDB(newReminder))
if err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"fmt"
"github.com/lucasnevespereira/lembra/internal/pkg/repository"
"github.com/lucasnevespereira/lembra/internal/pkg/repository/database"
"github.com/lucasnevespereira/lembra/internal/utils/logger"
"github.com/spf13/cobra"
)

Expand All @@ -23,18 +25,19 @@ func deleteReminder(cmd *cobra.Command, args []string) error {
id, _ := cmd.Flags().GetString("id")
deleteAll, _ := cmd.Flags().GetBool("all")

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

if deleteAll {
err = reminderRepo.DeleteAll()
if err != nil {
return err
}

fmt.Println("All reminders deleted successfully")
logger.Log.Infoln("All reminders deleted successfully")
} else {
existingReminder, err := reminderRepo.GetByID(id)
if err != nil {
Expand Down
26 changes: 17 additions & 9 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package cmd
import (
"fmt"
"github.com/lucasnevespereira/lembra/internal/pkg/repository"
"github.com/lucasnevespereira/lembra/internal/pkg/repository/database"
"github.com/lucasnevespereira/lembra/internal/utils/mapping"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"os"
)

var listCommand = &cobra.Command{
Expand All @@ -18,24 +22,28 @@ func init() {
}

func listReminders(cmd *cobra.Command, args []string) error {
reminderRepo, err := repository.NewReminderRepository()

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

reminders, err := reminderRepo.GetAll()
dbReminders, err := reminderRepo.GetAll()
if err != nil {
return err
}
reminders := mapping.ToRemindersDTO(dbReminders)

table := tablewriter.NewWriter(os.Stdout)
// Set the table header
table.SetHeader([]string{"ID", "Title", "Message", "Time"})

// Print the reminders
for _, r := range reminders {
fmt.Printf("ID: %s\n", r.ID)
fmt.Printf("Title: %s\n", r.Title)
fmt.Printf("Message: %s\n", r.Message)
fmt.Printf("Time: %s\n", r.Time)
fmt.Println("------")
table.Append([]string{r.ID, r.Title, r.Message, r.Time})
}

table.Render()

return nil
}
16 changes: 9 additions & 7 deletions cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ 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/utils/logger"
"github.com/robfig/cron"
"github.com/sevlyar/go-daemon"
"github.com/spf13/cobra"
"log"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -34,26 +35,27 @@ func runDaemon(cmd *cobra.Command, args []string) {

child, err := daemonContext.Reborn()
if err != nil {
log.Fatalf("failed to start the daemon: %v", err)
logger.Log.Fatalf("failed to start the daemon: %v", err)
}
if child != nil {
os.Exit(0)
}
defer func(daemonContext *daemon.Context) {
err := daemonContext.Release()
if err != nil {
log.Printf("failed to release daemon ressources: %v", err)
logger.Log.Errorf("failed to release daemon ressources: %v", err)
}
}(daemonContext)

reminderRepository, err := repository.NewReminderRepository()
db, err := database.Open()
if err != nil {
log.Fatalf("creating repository: %v", err)
logger.Log.Errorf("open db connection: %v\n", err)
}
reminderRepo := repository.NewReminderRepository(db)

notifier := notifier.NewCronNotifier(reminderRepository, cron.New())
notifier := notifier.NewCronNotifier(reminderRepo, cron.New())
if err := notifier.Start(); err != nil {
log.Fatalf("starting notifier: %v", err)
logger.Log.Fatalf("starting notifier: %v", err)
}

// Wait for termination signals
Expand Down
8 changes: 4 additions & 4 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cmd
import (
"bufio"
"fmt"
"github.com/lucasnevespereira/lembra/internal/utils/logger"
"github.com/spf13/cobra"
"log"
"os"
"path/filepath"
)
Expand All @@ -23,12 +23,12 @@ func readLog(cmd *cobra.Command, args []string) {
logFilePath := "lembra.log"
absPath, err := filepath.Abs(logFilePath)
if err != nil {
log.Fatalf("failed to get absolute path: %v", err)
logger.Log.Fatalf("failed to get absolute path: %v", err)
}

file, err := os.Open(absPath)
if err != nil {
log.Fatalf("failed to open log file: %v", err)
logger.Log.Fatalf("failed to open log file: %v", err)
}
defer file.Close()

Expand All @@ -38,6 +38,6 @@ func readLog(cmd *cobra.Command, args []string) {
}

if err := scanner.Err(); err != nil {
log.Fatalf("error reading log file: %v", err)
logger.Log.Fatalf("error reading log file: %v", err)
}
}
10 changes: 5 additions & 5 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cmd

import (
"fmt"
"github.com/lucasnevespereira/lembra/internal/utils/logger"
"github.com/sevlyar/go-daemon"
"github.com/spf13/cobra"
"log"
"os"
)

Expand All @@ -30,22 +30,22 @@ func stopDaemon(cmd *cobra.Command, args []string) {

daemonPid, err := daemonContext.Search()
if err != nil {
log.Fatalf("failed to search for daemon process: %v", err)
logger.Log.Fatalf("failed to search for daemon process: %v", err)
}

err = daemonPid.Signal(os.Interrupt)
if err != nil {
log.Fatalf("failed to send termination signal to the daemon: %v", err)
logger.Log.Fatalf("failed to send termination signal to the daemon: %v", err)
}

err = daemonContext.Release()
if err != nil {
log.Fatalf("failed to release daemon resources: %v", err)
logger.Log.Fatalf("failed to release daemon resources: %v", err)
}

err = os.Remove(daemonContext.LogFileName)
if err != nil {
log.Fatalf("failed to delete lembra process logs: %v", err)
logger.Log.Fatalf("failed to delete lembra process logs: %v", err)
}

fmt.Println("Lembra Daemon stopped")
Expand Down
7 changes: 5 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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/spf13/cobra"
)

Expand All @@ -19,6 +20,7 @@ func init() {
updateCommand.PersistentFlags().String("id", "", "Reminder ID")
updateCommand.PersistentFlags().String("title", "", "Notification Title")
updateCommand.PersistentFlags().String("message", "", "Notification Message")
updateCommand.PersistentFlags().String("time", "", "Time of the reminder (format: 2006-01-02 15:04)")

// Mark the required flags
_ = updateCommand.MarkFlagRequired("id")
Expand All @@ -30,10 +32,11 @@ func updateReminder(cmd *cobra.Command, args []string) error {
message, _ := cmd.Flags().GetString("message")
timeStr, _ := cmd.Flags().GetString("time")

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

existingReminder, err := reminderRepo.GetByID(id)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ require (
github.com/mattn/go-sqlite3 v1.14.16
github.com/robfig/cron v1.2.0
github.com/sevlyar/go-daemon v0.1.6
github.com/sirupsen/logrus v1.9.2
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.3
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
gorm.io/driver/sqlite v1.5.1
gorm.io/gorm v1.25.1
)
Expand All @@ -22,7 +24,9 @@ require (
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/mattn/go-runewidth v0.0.9 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/olekukonko/tablewriter v0.0.5 // 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
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gen2brain/beeep v0.0.0-20230307103607-6e717729cb4f h1:oRm7Hy2dQWfHgOuOWRaYZf+kZcWJst7fxAlq+yjdLss=
Expand All @@ -17,30 +18,47 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sevlyar/go-daemon v0.1.6 h1:EUh1MDjEM4BI109Jign0EaknA2izkOyi0LV3ro3QQGs=
github.com/sevlyar/go-daemon v0.1.6/go.mod h1:6dJpPatBT9eUwM5VCw9Bt6CdX9Tk6UWvhW3MebLDRKE=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y=
github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.1 h1:hYyrLkAWE71bcarJDPdZNTLWtr8XrSjOWyjUYI6xdL4=
Expand Down
Loading

0 comments on commit 4b88c0f

Please sign in to comment.