| 1 |
package revctrl |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 8 |
"git.sr.ht/~rjarry/aerc/lib/pama/models" |
| 9 |
) |
| 10 |
|
| 11 |
var ErrUnsupported = errors.New("unsupported") |
| 12 |
|
| 13 |
type factoryFunc func(string) models.RevisionController |
| 14 |
|
| 15 |
var controllers = map[string]factoryFunc{} |
| 16 |
|
| 17 |
func register(controllerID string, fn factoryFunc) { |
| 18 |
controllers[controllerID] = fn |
| 19 |
} |
| 20 |
|
| 21 |
func New(controllerID string, path string) (models.RevisionController, error) { |
| 22 |
factoryFunc, ok := controllers[controllerID] |
| 23 |
if !ok { |
| 24 |
return nil, errors.New("cannot create revision control instance") |
| 25 |
} |
| 26 |
return factoryFunc(path), nil |
| 27 |
} |
| 28 |
|
| 29 |
type detector interface { |
| 30 |
Support() bool |
| 31 |
Root() (string, error) |
| 32 |
} |
| 33 |
|
| 34 |
func Detect(path string) (string, string, error) { |
| 35 |
for controllerID, factoryFunc := range controllers { |
| 36 |
rc, ok := factoryFunc(path).(detector) |
| 37 |
if ok && rc.Support() { |
| 38 |
log.Tracef("support found for %v", controllerID) |
| 39 |
root, err := rc.Root() |
| 40 |
if err != nil { |
| 41 |
continue |
| 42 |
} |
| 43 |
log.Tracef("root found in %s", root) |
| 44 |
return controllerID, root, nil |
| 45 |
} |
| 46 |
} |
| 47 |
return "", "", fmt.Errorf("no supported repository found in %s", path) |
| 48 |
} |