Moves database initialization to OnInitialize
This commit is contained in:
parent
f7e246b7c5
commit
c0b6ecbf1c
1 changed files with 12 additions and 15 deletions
27
cmd/root.go
27
cmd/root.go
|
|
@ -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.
|
||||
`,
|
||||
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() {
|
||||
err := rootCmd.Execute()
|
||||
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
cobra.OnInitialize(initConfig, initDB)
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default: ~/.config/freed/config.toml)")
|
||||
}
|
||||
|
|
@ -115,3 +102,13 @@ func initConfig() {
|
|||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue