summaryrefslogtreecommitdiffstats
path: root/db/db.go
diff options
context:
space:
mode:
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
+}