diff options
author | siddharth <s@ricketyspace.net> | 2021-11-28 18:32:18 -0500 |
---|---|---|
committer | siddharth <s@ricketyspace.net> | 2021-11-28 18:32:18 -0500 |
commit | c1ace352e8ba09ec3c00317e72de6b871a8964ec (patch) | |
tree | 31da0ec7dca9a54d461e09cf2bbb81752317ed60 /db/db.go | |
parent | 0206d6e63427c168fe6f0fcdf2bee17e964a3d65 (diff) |
db: add FernDB.Write
Writes database to disk.
Diffstat (limited to 'db/db.go')
-rw-r--r-- | db/db.go | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -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 +} |