// Package driver abstracts the per-database connection details behind a small // interface so new engines can be added without touching the wait loop. package driver import ( "context" "fmt" "sort" "strings" "git.unkin.net/unkin/waitfordb/internal/config" ) // Pinger is a live handle to a database that can answer a trivial liveness // query. A successful Ping proves the server is up, auth succeeded, and the // target database/role exist. type Pinger interface { Ping(ctx context.Context) error Close() error } // Driver knows how to open a Pinger for one database engine. type Driver interface { Name() string Open(cfg config.Config) (Pinger, error) } var registry = map[string]Driver{} func register(d Driver) { registry[d.Name()] = d } // Get returns the registered driver for name. func Get(name string) (Driver, error) { d, ok := registry[strings.ToLower(name)] if !ok { return nil, fmt.Errorf("unsupported driver %q (supported: %s)", name, strings.Join(Names(), ", ")) } return d, nil } // Names lists the registered driver names, sorted. func Names() []string { out := make([]string, 0, len(registry)) for n := range registry { out = append(out, n) } sort.Strings(out) return out }