From 62648d1a4750298306807fa25a68a1769e22ba2c Mon Sep 17 00:00:00 2001 From: lucasnevespereira Date: Mon, 20 Nov 2023 01:08:37 +0100 Subject: [PATCH] refacto: introduce json storage --- .gitignore | 2 + .goreleaser.yaml | 6 +- cmd/create.go | 10 +- cmd/delete.go | 11 +- cmd/list.go | 10 +- cmd/listen.go | 13 +- cmd/update.go | 9 +- go.mod | 15 ++ go.sum | 123 ++++++++++++++ internal/pkg/notifier/notifier.go | 6 +- internal/pkg/repository/database/database.go | 29 ---- internal/pkg/repository/repository.go | 88 ---------- .../pkg/{repository => storage}/interface.go | 18 +-- internal/pkg/storage/open.go | 56 +++++++ internal/pkg/storage/storage.go | 151 ++++++++++++++++++ internal/utils/mapping/reminders_db.go | 6 +- internal/utils/mapping/reminders_dto.go | 6 +- 17 files changed, 387 insertions(+), 172 deletions(-) delete mode 100644 internal/pkg/repository/database/database.go delete mode 100644 internal/pkg/repository/repository.go rename internal/pkg/{repository => storage}/interface.go (58%) create mode 100644 internal/pkg/storage/open.go create mode 100644 internal/pkg/storage/storage.go diff --git a/.gitignore b/.gitignore index 7534bbd..df7aeb1 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ lembra.log lembra.pid coverage.out dist/ +remindersdb +reminders.json diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 68dc284..04b89d9 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -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 diff --git a/cmd/create.go b/cmd/create.go index 583e488..44cee11 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -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" @@ -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 { diff --git a/cmd/delete.go b/cmd/delete.go index 5d0ed80..d622c50 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -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" ) @@ -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 } diff --git a/cmd/list.go b/cmd/list.go index 9848323..cb26632 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -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" @@ -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 { diff --git a/cmd/listen.go b/cmd/listen.go index 180c585..d8da611 100644 --- a/cmd/listen.go +++ b/cmd/listen.go @@ -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" @@ -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) diff --git a/cmd/update.go b/cmd/update.go index 13e04c7..2a2d23c 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -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" ) @@ -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 { diff --git a/go.mod b/go.mod index 5da0989..a23cb55 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 21caba6..480fad2 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,54 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= +github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= 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/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg= +github.com/dgraph-io/badger/v3 v3.2103.5/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw= +github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= +github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA= +github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/gen2brain/beeep v0.0.0-20230307103607-6e717729cb4f h1:oRm7Hy2dQWfHgOuOWRaYZf+kZcWJst7fxAlq+yjdLss= github.com/gen2brain/beeep v0.0.0-20230307103607-6e717729cb4f/go.mod h1:0W7dI87PvXJ1Sjs0QPvWXKcQmNERY77e8l7GFhZB/s4= github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 h1:qZNfIGkIANxGv/OqtnntR4DfOY2+BgwR60cAcu/i3SE= github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4/go.mod h1:kW3HQ4UdaAyrUCSSDR4xUzBKW6O2iA4uHhk7AtyYp10= github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/flatbuffers v1.12.1 h1:MVlul7pQNoDzWRLTw5imwYsl+usrS1TXG2H4jg6ImGw= +github.com/google/flatbuffers v1.12.1/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= @@ -18,32 +57,55 @@ 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/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.12.3 h1:G5AfA94pHPysR56qqrkO2pxEexdDzrpFJ6yt/VqWxVU= +github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= 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/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= 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/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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 v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= 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/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= 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/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 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/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= 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.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 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= @@ -51,13 +113,73 @@ github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1 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= +github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0= +go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14/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= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= 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/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 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= @@ -65,3 +187,4 @@ gorm.io/driver/sqlite v1.5.1 h1:hYyrLkAWE71bcarJDPdZNTLWtr8XrSjOWyjUYI6xdL4= gorm.io/driver/sqlite v1.5.1/go.mod h1:7MZZ2Z8bqyfSQA1gYEV6MagQWj3cpUkJj9Z+d1HEMEQ= gorm.io/gorm v1.25.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64= gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/pkg/notifier/notifier.go b/internal/pkg/notifier/notifier.go index eb119a6..3bcca44 100644 --- a/internal/pkg/notifier/notifier.go +++ b/internal/pkg/notifier/notifier.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/gen2brain/beeep" "github.com/lucasnevespereira/lembra/internal/pkg/reminder" - "github.com/lucasnevespereira/lembra/internal/pkg/repository" + "github.com/lucasnevespereira/lembra/internal/pkg/storage" "github.com/lucasnevespereira/lembra/internal/utils/logger" "github.com/lucasnevespereira/lembra/internal/utils/mapping" "github.com/robfig/cron" @@ -12,11 +12,11 @@ import ( ) type CronNotifier struct { - repo repository.ReminderRepository + repo storage.ReminderStorage cron *cron.Cron } -func NewCronNotifier(repository repository.ReminderRepository, cron *cron.Cron) *CronNotifier { +func NewCronNotifier(repository storage.ReminderStorage, cron *cron.Cron) *CronNotifier { return &CronNotifier{ repo: repository, cron: cron, diff --git a/internal/pkg/repository/database/database.go b/internal/pkg/repository/database/database.go deleted file mode 100644 index 8d6e9f0..0000000 --- a/internal/pkg/repository/database/database.go +++ /dev/null @@ -1,29 +0,0 @@ -package database - -import ( - "gorm.io/driver/sqlite" - "gorm.io/gorm" - "os" -) - -func Open() (*gorm.DB, error) { - dbPath := "reminders.db" - - // Check if the database file exists - _, err := os.Stat(dbPath) - if os.IsNotExist(err) { - // Create the database file if it doesn't exist - _, err := os.Create(dbPath) - if err != nil { - return nil, err - } - } - - // Open the database connection - db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) - if err != nil { - return nil, err - } - - return db, nil -} diff --git a/internal/pkg/repository/repository.go b/internal/pkg/repository/repository.go deleted file mode 100644 index 0378d4f..0000000 --- a/internal/pkg/repository/repository.go +++ /dev/null @@ -1,88 +0,0 @@ -package repository - -import ( - "fmt" - "github.com/lucasnevespereira/lembra/internal/utils/logger" - "gorm.io/gorm" -) - -type repository struct { - db *gorm.DB -} - -func NewReminderRepository(db *gorm.DB) ReminderRepository { - err := db.AutoMigrate(&ReminderDB{}) - if err != nil { - logger.Log.Printf("failed to migrate reminders table: %v", err) - } - return &repository{ - db: db, - } -} - -func (r *repository) Create(reminder *ReminderDB) error { - result := r.db.Create(reminder) - if result.Error != nil { - return fmt.Errorf("failed to create reminder: %v", result.Error) - } - return nil -} - -func (r *repository) GetAll() ([]*ReminderDB, error) { - var reminders []*ReminderDB - result := r.db.Find(&reminders) - if result.Error != nil { - return nil, fmt.Errorf("failed to retrieve reminders: %v", result.Error) - } - return reminders, nil -} - -func (r *repository) GetByID(id string) (*ReminderDB, error) { - var reminder *ReminderDB - result := r.db.First(&reminder, "id = ?", id) - if result.Error != nil { - return nil, fmt.Errorf("failed to retrieve reminder with id %s: %v", id, result.Error) - } - - return reminder, nil -} - -func (r *repository) Update(reminder *ReminderDB) error { - result := r.db.Save(reminder) - if result.Error != nil { - return fmt.Errorf("failed to update reminder: %v", result.Error) - } - return nil -} - -func (r *repository) UpdateNotified(id string, value bool) error { - result := r.db.Model(&ReminderDB{}).Where("id = ?", id).Update("notified", value) - if result.Error != nil { - return fmt.Errorf("failed to update reminder: %v", result.Error) - } - return nil -} - -func (r *repository) DeleteByID(id string) error { - result := r.db.Delete(&ReminderDB{}, "id = ?", id) - if result.Error != nil { - return fmt.Errorf("failed to delete reminder with id %s: %v", id, result.Error) - } - return nil -} - -func (r *repository) DeleteAll() error { - result := r.db.Where("id not null").Delete(&ReminderDB{}) - if result.Error != nil { - return fmt.Errorf("failed to delete all reminders: %v", result.Error) - } - return nil -} - -func (r *repository) DeleteNotified() error { - result := r.db.Delete(&ReminderDB{}, "notified = ?", true) - if result.Error != nil { - return fmt.Errorf("failed to delete notified reminders: %v", result.Error) - } - return nil -} diff --git a/internal/pkg/repository/interface.go b/internal/pkg/storage/interface.go similarity index 58% rename from internal/pkg/repository/interface.go rename to internal/pkg/storage/interface.go index 5458b48..39a8dce 100644 --- a/internal/pkg/repository/interface.go +++ b/internal/pkg/storage/interface.go @@ -1,6 +1,6 @@ -package repository +package storage -type ReminderRepository interface { +type ReminderStorage interface { Create(reminder *ReminderDB) error GetAll() ([]*ReminderDB, error) GetByID(id string) (*ReminderDB, error) @@ -12,13 +12,9 @@ type ReminderRepository interface { } type ReminderDB struct { - ID string - Title string - Message string - Time string - Notified bool -} - -func (ReminderDB) TableName() string { - return "reminders" + ID string `json:ID` + Title string `json:"Title"` + Message string `json:"Message"` + Time string `json:"Time"` + Notified bool `json:"Notified"` } diff --git a/internal/pkg/storage/open.go b/internal/pkg/storage/open.go new file mode 100644 index 0000000..f83387c --- /dev/null +++ b/internal/pkg/storage/open.go @@ -0,0 +1,56 @@ +package storage + +import ( + "github.com/pkg/errors" + "os" +) + +const remindersDBFileName = "reminders.json" + +func OpenStorageFile() (string, error) { + dataFile, err := getLocalFile(remindersDBFileName) + if err != nil { + return "", errors.Wrap(err, "OpenStorageFile") + } + + return dataFile, nil +} + +func getLocalFile(filename string) (string, error) { + localConfigDir, err := ensureLocalConfigDir() + if err != nil { + return "", errors.Wrap(err, "checkFile - UserHomeDir") + } + + localDataFile := localConfigDir + filename + _, err = os.Stat(localDataFile) + if os.IsNotExist(err) { + createdFile, err := os.Create(localDataFile) + if err != nil { + return "", errors.Wrap(err, "checkFile - create") + } + + _, err = createdFile.Write([]byte("[]")) + if err != nil { + return "", errors.Wrap(err, "checkFile - write") + } + + } + return localDataFile, nil +} + +func ensureLocalConfigDir() (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + configDir := homeDir + "/.config/lembra/" + if _, err := os.Stat(configDir); errors.Is(err, os.ErrNotExist) { + err := os.MkdirAll(configDir, os.ModePerm) + if err != nil { + return "", err + } + } + + return configDir, nil +} diff --git a/internal/pkg/storage/storage.go b/internal/pkg/storage/storage.go new file mode 100644 index 0000000..ca0141b --- /dev/null +++ b/internal/pkg/storage/storage.go @@ -0,0 +1,151 @@ +package storage + +import ( + "encoding/json" + "os" +) + +type storage struct { + filePath string +} + +func NewReminderStorage(filePath string) ReminderStorage { + return &storage{ + filePath: filePath, + } +} + +func (s *storage) Create(reminder *ReminderDB) error { + reminders, err := s.GetAll() + if err != nil { + return err + } + + reminders = append(reminders, reminder) + + data, err := json.MarshalIndent(reminders, "", " ") + if err != nil { + return err + } + + return os.WriteFile(s.filePath, data, 0644) +} + +func (s *storage) GetAll() ([]*ReminderDB, error) { + data, err := os.ReadFile(s.filePath) + if err != nil { + return nil, err + } + + var reminders []*ReminderDB + err = json.Unmarshal(data, &reminders) + if err != nil { + return nil, err + } + + return reminders, nil +} + +func (s *storage) GetByID(id string) (*ReminderDB, error) { + reminders, err := s.GetAll() + if err != nil { + return nil, err + } + + for _, reminder := range reminders { + if reminder.ID == id { + return reminder, nil + } + } + + return nil, nil +} + +func (s *storage) Update(reminder *ReminderDB) error { + reminders, err := s.GetAll() + if err != nil { + return err + } + + for i, existingReminder := range reminders { + if existingReminder.ID == reminder.ID { + reminders[i] = reminder + data, err := json.MarshalIndent(reminders, "", " ") + if err != nil { + return err + } + + return os.WriteFile(s.filePath, data, 0644) + } + } + + return nil +} + +func (s *storage) UpdateNotified(id string, value bool) error { + reminders, err := s.GetAll() + if err != nil { + return err + } + + for _, reminder := range reminders { + if reminder.ID == id { + reminder.Notified = value + data, err := json.MarshalIndent(reminders, "", " ") + if err != nil { + return err + } + + return os.WriteFile(s.filePath, data, 0644) + } + } + + return nil +} + +func (s *storage) DeleteByID(id string) error { + reminders, err := s.GetAll() + if err != nil { + return err + } + + var updatedReminders []*ReminderDB + for _, reminder := range reminders { + if reminder.ID != id { + updatedReminders = append(updatedReminders, reminder) + } + } + + data, err := json.MarshalIndent(updatedReminders, "", " ") + if err != nil { + return err + } + + return os.WriteFile(s.filePath, data, 0644) +} + +func (s *storage) DeleteAll() error { + // Simply create an empty file to delete all reminders + return os.WriteFile(s.filePath, []byte("[]"), 0644) +} + +func (s *storage) DeleteNotified() error { + reminders, err := s.GetAll() + if err != nil { + return err + } + + var updatedReminders []*ReminderDB + for _, reminder := range reminders { + if !reminder.Notified { + updatedReminders = append(updatedReminders, reminder) + } + } + + data, err := json.MarshalIndent(updatedReminders, "", " ") + if err != nil { + return err + } + + return os.WriteFile(s.filePath, data, 0644) +} diff --git a/internal/utils/mapping/reminders_db.go b/internal/utils/mapping/reminders_db.go index a967cb1..be5c222 100644 --- a/internal/utils/mapping/reminders_db.go +++ b/internal/utils/mapping/reminders_db.go @@ -2,11 +2,11 @@ package mapping import ( "github.com/lucasnevespereira/lembra/internal/pkg/reminder" - "github.com/lucasnevespereira/lembra/internal/pkg/repository" + "github.com/lucasnevespereira/lembra/internal/pkg/storage" ) -func ToReminderDB(reminder *reminder.Reminder) *repository.ReminderDB { - return &repository.ReminderDB{ +func ToReminderDB(reminder *reminder.Reminder) *storage.ReminderDB { + return &storage.ReminderDB{ ID: reminder.ID, Title: reminder.Title, Message: reminder.Message, diff --git a/internal/utils/mapping/reminders_dto.go b/internal/utils/mapping/reminders_dto.go index 52a909e..a4b0bf1 100644 --- a/internal/utils/mapping/reminders_dto.go +++ b/internal/utils/mapping/reminders_dto.go @@ -2,10 +2,10 @@ package mapping import ( "github.com/lucasnevespereira/lembra/internal/pkg/reminder" - "github.com/lucasnevespereira/lembra/internal/pkg/repository" + "github.com/lucasnevespereira/lembra/internal/pkg/storage" ) -func ToReminderDTO(reminderDB *repository.ReminderDB) *reminder.Reminder { +func ToReminderDTO(reminderDB *storage.ReminderDB) *reminder.Reminder { return &reminder.Reminder{ ID: reminderDB.ID, Title: reminderDB.Title, @@ -15,7 +15,7 @@ func ToReminderDTO(reminderDB *repository.ReminderDB) *reminder.Reminder { } } -func ToRemindersDTO(reminderDBs []*repository.ReminderDB) []*reminder.Reminder { +func ToRemindersDTO(reminderDBs []*storage.ReminderDB) []*reminder.Reminder { reminders := make([]*reminder.Reminder, len(reminderDBs)) for i, reminderDB := range reminderDBs { reminders[i] = ToReminderDTO(reminderDB)