From 4436b83a157e4c4ac27f96504244bef71f904a40 Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 3 Sep 2024 19:49:38 +0200 Subject: [PATCH 1/2] Adapts database initialization --- cmd/root.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d31aa54..05d1d33 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,6 @@ along with this program. If not, see . package cmd import ( - "database/sql" "fmt" "freed/internal/database" "os" @@ -37,7 +36,6 @@ type Config struct { type AppContext struct { Config Config - DB *sql.DB } var ( @@ -67,7 +65,6 @@ func Execute() { func init() { cobra.OnInitialize(initConfig, initDB) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)") } @@ -104,11 +101,8 @@ func initConfig() { } func initDB() { - db, err := database.Connect(appContext.Config.DB.Path) - - if err != nil { - fmt.Printf("Could not connect to the database: %s", err) + if err := database.Open(appContext.Config.DB.Path); err != nil { + fmt.Printf("Could not connect to database: %s", err) + os.Exit(1) } - - appContext.DB = db } From 9fdc8cad49fabcfb7504661ed39dc85aca2a9be2 Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 3 Sep 2024 19:52:58 +0200 Subject: [PATCH 2/2] Fixes db init call in root cmd --- cmd/root.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 05d1d33..5397fa5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -65,6 +65,7 @@ func Execute() { func init() { cobra.OnInitialize(initConfig, initDB) + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)") } @@ -101,8 +102,9 @@ func initConfig() { } func initDB() { - if err := database.Open(appContext.Config.DB.Path); err != nil { - fmt.Printf("Could not connect to database: %s", err) - os.Exit(1) + err := database.Open(appContext.Config.DB.Path) + + if err != nil { + fmt.Printf("Could not connect to the database: %s", err) } }