Auto-remediate style-only findings (wsl_v5, nlreturn, noinlineerr, modernize, intrange, perfsprint, usetesting, unconvert, errorlint, gocritic, testifylint) and rename printf-style helpers to f-suffixed names (goprintffuncname): ui.Writer message methods, cli.ReportErrorf, database.Fatalf, vaultik stdoutf.
53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"go.uber.org/fx"
|
|
"sneak.berlin/go/vaultik/internal/config"
|
|
"sneak.berlin/go/vaultik/internal/log"
|
|
)
|
|
|
|
// Module provides database dependencies
|
|
var Module = fx.Module("database",
|
|
fx.Provide(
|
|
provideDatabase,
|
|
NewRepositories,
|
|
),
|
|
)
|
|
|
|
func provideDatabase(lc fx.Lifecycle, cfg *config.Config) (*DB, error) {
|
|
// Ensure the index directory exists
|
|
indexDir := filepath.Dir(cfg.IndexPath)
|
|
if err := os.MkdirAll(indexDir, 0700); err != nil {
|
|
return nil, fmt.Errorf("creating index directory: %w", err)
|
|
}
|
|
|
|
db, err := New(context.Background(), cfg.IndexPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("opening database: %w", err)
|
|
}
|
|
|
|
lc.Append(fx.Hook{
|
|
OnStop: func(ctx context.Context) error {
|
|
log.Debug("Database module OnStop hook called")
|
|
|
|
err := db.Close()
|
|
if err != nil {
|
|
log.Error("Failed to close database in OnStop hook", "error", err)
|
|
|
|
return err
|
|
}
|
|
|
|
log.Debug("Database closed successfully in OnStop hook")
|
|
|
|
return nil
|
|
},
|
|
})
|
|
|
|
return db, nil
|
|
}
|