2025-01-04 09:40:43 +01:00
|
|
|
package storage
|
|
|
|
|
2025-01-04 10:37:12 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2025-01-04 09:40:43 +01:00
|
|
|
const (
|
|
|
|
DBTYPE_MEM = iota
|
|
|
|
DBTYPE_GDBM
|
|
|
|
DBTYPE_POSTGRES
|
|
|
|
)
|
|
|
|
|
|
|
|
type connData struct {
|
|
|
|
typ int
|
|
|
|
str string
|
|
|
|
}
|
|
|
|
|
2025-01-04 10:37:12 +01:00
|
|
|
func (cd *connData) DbType() int {
|
|
|
|
return cd.typ
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *connData) String() string {
|
|
|
|
return cd.str
|
|
|
|
}
|
|
|
|
|
|
|
|
func probePostgres(s string) (string, bool) {
|
|
|
|
v, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
if v.Scheme != "postgres" {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return s, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func probeGdbm(s string) (string, bool) {
|
|
|
|
if !path.IsAbs(s) {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
s = path.Clean(s)
|
|
|
|
return s, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func toConnData(connStr string) (connData, error) {
|
2025-01-04 09:40:43 +01:00
|
|
|
var o connData
|
|
|
|
|
2025-01-04 10:37:12 +01:00
|
|
|
if connStr == "" {
|
|
|
|
return o, nil
|
2025-01-04 09:40:43 +01:00
|
|
|
}
|
2025-01-04 10:37:12 +01:00
|
|
|
|
|
|
|
v, ok := probePostgres(connStr)
|
|
|
|
if ok {
|
|
|
|
o.typ = DBTYPE_POSTGRES
|
|
|
|
o.str = v
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok = probeGdbm(connStr)
|
|
|
|
if ok {
|
|
|
|
o.typ = DBTYPE_GDBM
|
|
|
|
o.str = v
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return o, fmt.Errorf("invalid connection string: %s", connStr)
|
2025-01-04 09:40:43 +01:00
|
|
|
}
|