From 4355a79ec025e80f2309c5a610a40ec71e6be91d Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 23 Jan 2025 08:36:25 -0800 Subject: [PATCH] Refactor db open to helper func --- cmd/feed.go | 8 +------- cmd/itemAdd.go | 10 ++-------- cmd/itemDeactivate.go | 8 +------- cmd/migrate.go | 5 +---- cmd/root.go | 23 +++++++++++++++++++++++ 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/cmd/feed.go b/cmd/feed.go index fc21f88..35eda59 100644 --- a/cmd/feed.go +++ b/cmd/feed.go @@ -43,13 +43,7 @@ func feed() { log.Fatal(err) } - db, err := core.OpenDb(getDbPath()) - if err != nil { - log.Fatalf("error: failed to open %s", dbPath) - } - - core.InitDatabase(db) - core.MigrateDatabase(db) + db := openAndMigrateDb() var items []core.Item if feedSource != "" { diff --git a/cmd/itemAdd.go b/cmd/itemAdd.go index 15c6746..b6b2470 100644 --- a/cmd/itemAdd.go +++ b/cmd/itemAdd.go @@ -57,15 +57,9 @@ func itemAdd() { addId = hex.EncodeToString(bytes) } - db, err := core.OpenDb(getDbPath()) - if err != nil { - log.Fatalf("Failed to open %s", dbPath) - } + db := openAndMigrateDb() - core.InitDatabase(db) - core.MigrateDatabase(db) - - err = core.AddItem(db, addSource, addId, addTitle, addAuthor, addBody, addLink, addTime) + err := core.AddItem(db, addSource, addId, addTitle, addAuthor, addBody, addLink, addTime) if err != nil { log.Fatalf("Failed to add item: %s", err) } diff --git a/cmd/itemDeactivate.go b/cmd/itemDeactivate.go index 9aab315..eeb869e 100644 --- a/cmd/itemDeactivate.go +++ b/cmd/itemDeactivate.go @@ -34,13 +34,7 @@ func init() { } func itemDeactivate() { - db, err := core.OpenDb(getDbPath()) - if err != nil { - log.Fatalf("Failed to open %s", dbPath) - } - - core.InitDatabase(db) - core.MigrateDatabase(db) + db := openAndMigrateDb() active, err := core.DeactivateItem(db, deacSource, deacItem) if err != nil { diff --git a/cmd/migrate.go b/cmd/migrate.go index 6fe4b73..6993a78 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -29,10 +29,7 @@ func init() { } func migrate() { - db, err := core.OpenDb(getDbPath()) - if err != nil { - log.Fatal(err) - } + db := openDb() core.InitDatabase(db) if migrateListOnly { diff --git a/cmd/root.go b/cmd/root.go index 86089f5..4c981f9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,8 +2,10 @@ package cmd import ( "fmt" + "log" "os" + "github.com/Jaculabilis/intake/core" "github.com/spf13/cobra" ) @@ -42,3 +44,24 @@ func getDbPath() string { os.Exit(1) return "" } + +// Attempt to open the specified database and exit with an error if it fails. +func openDb() *core.DB { + db, err := core.OpenDb(getDbPath()) + if err != nil { + log.Fatalf("error: Failed to open %s", dbPath) + } + return db +} + +// Attempt to open and migrate the specified database and exit with an error if it fails. +func openAndMigrateDb() *core.DB { + db := openDb() + if err := core.InitDatabase(db); err != nil { + log.Fatalf("error: Failed to init database: %v", err) + } + if err := core.MigrateDatabase(db); err != nil { + log.Fatalf("error: Failed to migrate database: %v", err) + } + return db +}