mirror of
https://git.sr.ht/~ashkeel/strimertul
synced 2024-09-18 01:50:50 +00:00
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
|
package database
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
"go.uber.org/zap"
|
||
|
|
||
|
kv "github.com/strimertul/kilovolt/v9"
|
||
|
"github.com/urfave/cli/v2"
|
||
|
)
|
||
|
|
||
|
// DatabaseDriver is a driver wrapping a supported database
|
||
|
type DatabaseDriver interface {
|
||
|
Hub() *kv.Hub
|
||
|
Close() error
|
||
|
Import(map[string]string) error
|
||
|
Export(io.Writer) error
|
||
|
Restore(io.Reader) error
|
||
|
Backup(io.Writer) error
|
||
|
}
|
||
|
|
||
|
type BackupOptions struct {
|
||
|
BackupDir string
|
||
|
BackupInterval int
|
||
|
MaxBackups int
|
||
|
}
|
||
|
|
||
|
const databaseDefaultDriver = "pebble"
|
||
|
|
||
|
func getDatabaseDriverName(ctx *cli.Context) string {
|
||
|
driver := ctx.String("driver")
|
||
|
if driver != "auto" {
|
||
|
return driver
|
||
|
}
|
||
|
|
||
|
dbDirectory := ctx.String("database-dir")
|
||
|
file, err := os.ReadFile(filepath.Join(dbDirectory, "stul-driver"))
|
||
|
if err != nil {
|
||
|
// No driver file found (or file corrupted), use default driver
|
||
|
return databaseDefaultDriver
|
||
|
}
|
||
|
return string(file)
|
||
|
}
|
||
|
|
||
|
func GetDatabaseDriver(ctx *cli.Context) (DatabaseDriver, error) {
|
||
|
name := getDatabaseDriverName(ctx)
|
||
|
dbDirectory := ctx.String("database-dir")
|
||
|
logger := ctx.Context.Value("logger").(*zap.Logger)
|
||
|
|
||
|
switch name {
|
||
|
case "badger":
|
||
|
return nil, cli.Exit("Badger is not supported anymore as a database driver", 64)
|
||
|
case "pebble":
|
||
|
return NewPebble(dbDirectory, logger)
|
||
|
default:
|
||
|
return nil, cli.Exit(fmt.Sprintf("Unknown database driver: %s", name), 64)
|
||
|
}
|
||
|
}
|