summaryrefslogtreecommitdiffstats
path: root/db/db.go
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-11-28 18:32:18 -0500
committersiddharth <s@ricketyspace.net>2021-11-28 18:32:18 -0500
commitc1ace352e8ba09ec3c00317e72de6b871a8964ec (patch)
tree31da0ec7dca9a54d461e09cf2bbb81752317ed60 /db/db.go
parent0206d6e63427c168fe6f0fcdf2bee17e964a3d65 (diff)
db: add FernDB.Write
Writes database to disk.
Diffstat (limited to 'db/db.go')
-rw-r--r--db/db.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/db/db.go b/db/db.go
index bbe5b36..4ee511b 100644
--- a/db/db.go
+++ b/db/db.go
@@ -96,3 +96,28 @@ func (fdb *FernDB) Add(feed, entry string) {
fdb.downloaded[feed] = append(fdb.downloaded[feed], entry)
fdb.mutex.Unlock()
}
+
+func (fdb *FernDB) Write() error {
+ if len(dbPath) == 0 {
+ return fmt.Errorf("FernDB path not set")
+ }
+
+ f, err := os.OpenFile(dbPath, os.O_WRONLY|os.O_CREATE, 0644)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+
+ // Marshal database into json.
+ bs, err := json.Marshal(fdb.downloaded)
+ if err != nil {
+ return err
+ }
+
+ // Write to disk.
+ _, err = f.Write(bs)
+ if err != nil {
+ return err
+ }
+ return nil
+}