From c71594f2adaf0e86b15d1f56d248ab906b48b49e Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Tue, 25 Oct 2022 14:39:17 -0400 Subject: [PATCH] fix crash on missing config --- db/db.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/db/db.go b/db/db.go index 1be722f..f3ff314 100644 --- a/db/db.go +++ b/db/db.go @@ -59,7 +59,7 @@ func (d *Db) Add(i Item) (int, bool) { func (d *Db) Transact(id int, persist bool, f func(*Item)) error { d.mutex.Lock() defer d.mutex.Unlock() - for i, _ := range d.items { + for i := range d.items { if d.items[i].Id == id { f(&d.items[i]) if persist { @@ -68,7 +68,7 @@ func (d *Db) Transact(id int, persist bool, f func(*Item)) error { return nil } } - return fmt.Errorf("Invalid id: %d", id) + return fmt.Errorf("invalid id: %d", id) } func (d *Db) save() error { @@ -93,11 +93,13 @@ func (d *Db) Save() error { func Load(path string) (*Db, error) { data, err := os.ReadFile(path) + indexMap := map[string]int{} if err != nil { if errors.Is(err, os.ErrNotExist) { return &Db{ path: path, lastId: 0, + index: indexMap, }, nil } return nil, err @@ -108,7 +110,6 @@ func Load(path string) (*Db, error) { return nil, err } m := 0 - indexMap := map[string]int{} for _, item := range jd.Items { if item.Id > m { m = item.Id