summaryrefslogtreecommitdiffstats
path: root/db/db.go
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-11-28 16:42:09 -0500
committersiddharth <s@ricketyspace.net>2021-11-28 16:42:09 -0500
commit8adac927363d1f541b0737c96c713cd6d79acb79 (patch)
treec106859f490655665f482fe154cfb2395d227dd3 /db/db.go
parentd9e64324dc5c6dc5fd9890db8ec1aaa957974032 (diff)
add db package
Diffstat (limited to 'db/db.go')
-rw-r--r--db/db.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/db/db.go b/db/db.go
new file mode 100644
index 0000000..2d6a4d5
--- /dev/null
+++ b/db/db.go
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: ISC
+// Copyright © 2021 siddharth <s@ricketyspace.net>
+
+package db
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+ "path"
+
+ "ricketyspace.net/fern/file"
+)
+
+var dbPath string
+
+type FernDB struct {
+ // Key: feed-id
+ // Value: feed-id's entries that were downloaded
+ downloaded map[string][]string
+}
+
+func init() {
+ dbPath = "" // Reset.
+
+ // Construct default dbPath
+ h, err := os.UserHomeDir()
+ if err != nil {
+ return
+ }
+ dbPath = path.Join(h, ".config", "fern", "db.json")
+
+}
+
+func Open() (*FernDB, error) {
+ if len(dbPath) == 0 {
+ return nil, fmt.Errorf("FernDB path not set")
+ }
+
+ // Check if db exists.
+ _, err := os.Stat(dbPath)
+ if err != nil {
+ // db does not exist yet; create an empty one.
+ db := new(FernDB)
+ db.downloaded = make(map[string][]string)
+ return db, nil
+ }
+
+ // Read db from disk.
+ f, err := os.Open(dbPath)
+ if err != nil {
+ return nil, err
+ }
+ bs, err := file.Read(f)
+ if err != nil {
+ return nil, err
+ }
+
+ // Unmarshal db into an object.
+ db := new(FernDB)
+ err = json.Unmarshal(bs, &db.downloaded)
+ if err != nil {
+ return nil, err
+ }
+ return db, nil
+}