strimertul/database/driver.interface.go

63 lines
1.3 KiB
Go

package database
import (
"fmt"
"io"
"os"
"path/filepath"
kv "git.sr.ht/~ashkeel/kilovolt/v12"
"github.com/urfave/cli/v2"
)
// Driver is a driver wrapping a supported database
type Driver 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) (Driver, error) {
name := getDatabaseDriverName(ctx)
dbDirectory := ctx.String("database-dir")
switch name {
case "badger":
return nil, cli.Exit("Badger is not supported anymore as a database driver", 64)
case "pebble":
db, err := NewPebble(dbDirectory)
if err != nil {
return nil, cli.Exit(err.Error(), 64)
}
return db, nil
default:
return nil, cli.Exit(fmt.Sprintf("Unknown database driver: %s", name), 64)
}
}