Moves database initialization to OnInitialize

This commit is contained in:
Dennis Schoepf 2024-09-03 18:21:18 +02:00
parent f7e246b7c5
commit c0b6ecbf1c

View file

@ -54,32 +54,19 @@ Where f[r]eed is different to other feed aggregators or bookmarking services is
The application also includes a set of recommended "Small Web" and topic-specific curated feeds. These are also available through the configuration. The application also includes a set of recommended "Small Web" and topic-specific curated feeds. These are also available through the configuration.
`, `,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
db, err := database.Connect(appContext.Config.DB.Path)
if err != nil {
fmt.Printf("Could not connect to the database: %s", err)
}
appContext.DB = db
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if appContext.DB != nil {
appContext.DB.Close()
}
},
} }
) )
func Execute() { func Execute() {
err := rootCmd.Execute() err := rootCmd.Execute()
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
} }
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig, initDB)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)")
} }
@ -115,3 +102,13 @@ func initConfig() {
os.Exit(1) os.Exit(1)
} }
} }
func initDB() {
db, err := database.Connect(appContext.Config.DB.Path)
if err != nil {
fmt.Printf("Could not connect to the database: %s", err)
}
appContext.DB = db
}