-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrivercore-drivers.go
56 lines (48 loc) · 1.36 KB
/
drivercore-drivers.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package drivercore
import (
"strings"
)
var drivers = map[string]Driver{}
// IsRegisteredDriver checks if a driver name is registered. The name
// check is case-insensitive.
func IsRegisteredDriver(name string) bool {
_, ok := drivers[strings.ToLower(name)]
return ok
}
// RegisteredDrivers returns all registered driver names.
func RegisteredDrivers() []string {
result := make([]string, 0, len(drivers))
for i := range drivers {
result = append(result, i)
}
return result
}
// DriverCount returns the number of registered drivers.
func DriverCount() int {
return len(drivers)
}
// ForEachDriver iterates over registered Drivers.
// The callback function can return false to stop the iteration.
func ForEachDriver(f func(Driver) bool) {
for _, driver := range drivers {
ok := f(driver)
if !ok {
break
}
}
}
// GetDriver returns a Driver corresponding to the name.
// If there is no driver registered against the name, nil is returned.
// Names are case-insensitive.
func GetDriver(name string) (Driver, bool) {
result, ok := drivers[strings.ToLower(name)]
return result, ok
}
// RegisterDriver registers a Driver with a name to drivercore.
// If a driver with the specified name already exists, it is replaced.
// Driver names are converted to lower case.
func RegisterDriver(name string, d Driver) {
if d != nil {
drivers[strings.ToLower(name)] = d
}
}