Print sqlite database on startup

This commit is contained in:
andig 2022-12-11 11:28:08 +01:00
parent ae393838f4
commit 68ac3c0f51
3 changed files with 18 additions and 7 deletions

View file

@ -40,14 +40,22 @@ const (
flagDelay = "delay"
)
func bind(cmd *cobra.Command, flag string) {
if err := viper.BindPFlag(flag, cmd.Flags().Lookup(flag)); err != nil {
func bind(cmd *cobra.Command, key string, flagName ...string) {
name := key
if len(flagName) == 1 {
name = flagName[0]
}
if err := viper.BindPFlag(key, cmd.Flags().Lookup(name)); err != nil {
panic(err)
}
}
func bindP(cmd *cobra.Command, flag string) {
if err := viper.BindPFlag(flag, cmd.PersistentFlags().Lookup(flag)); err != nil {
func bindP(cmd *cobra.Command, key string, flagName ...string) {
name := key
if len(flagName) == 1 {
name = flagName[0]
}
if err := viper.BindPFlag(key, cmd.PersistentFlags().Lookup(name)); err != nil {
panic(err)
}
}

View file

@ -56,9 +56,10 @@ func init() {
rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
rootCmd.PersistentFlags().String(flagSqlite, "", flagSqliteDescription)
// config file options
rootCmd.PersistentFlags().String(flagSqlite, "", flagSqliteDescription)
bindP(rootCmd, "database.dsn", flagSqlite)
rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)")
bindP(rootCmd, "log")

View file

@ -16,6 +16,7 @@ var Instance *gorm.DB
func New(driver, dsn string) (*gorm.DB, error) {
var dialect gorm.Dialector
log := util.NewLogger("db")
switch driver {
case "sqlite":
@ -26,6 +27,7 @@ func New(driver, dsn string) (*gorm.DB, error) {
if err := os.MkdirAll(filepath.Dir(file), os.ModePerm); err != nil {
return nil, err
}
log.INFO.Println("using sqlite database:", file)
// avoid busy errors
dialect = sqlite.Open(file + "?_pragma=busy_timeout(5000)")
// case "postgres":
@ -37,7 +39,7 @@ func New(driver, dsn string) (*gorm.DB, error) {
}
return gorm.Open(dialect, &gorm.Config{
Logger: &Logger{util.NewLogger("db")},
Logger: &Logger{log},
})
}