/
driver.go
39 lines (32 loc) 路 1.06 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package web
import (
"strings"
"github.com/johnstarich/sage/search"
"github.com/pkg/errors"
)
type Driver func(CredConnector) (Connector, error)
var driverFuncs = make(map[string]Driver)
// Connect creates a Connector with the given driver name and credentials
func Connect(connector CredConnector) (Connector, error) {
name := strings.ToLower(connector.Driver())
driver, exists := driverFuncs[name]
if !exists {
return nil, errors.Errorf("Driver does not exist with name: %q", name)
}
return driver(connector)
}
// Register adds a driver with the given name to the registry. Enables a call with Connect and the same driver name
func Register(name string, driver Driver) {
name = strings.ToLower(name)
if _, exists := driverFuncs[name]; exists {
panic("Driver with duplicate name registered: " + name)
}
driverFuncs[name] = driver
}
func Search(query string) []string {
driverNames := make([]string, 0, len(driverFuncs))
for driver := range driverFuncs {
driverNames = append(driverNames, strings.Title(driver))
}
return search.Query(driverNames, query)
}