aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pama/revctrl/revctrl.go
blob: 42532216b9b03bf059200b828bb501e7cca9a796 (plain) (blame)
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
package revctrl

import (
	"errors"
	"fmt"

	"git.sr.ht/~rjarry/aerc/lib/pama/models"
	"git.sr.ht/~rjarry/aerc/log"
)

var ErrUnsupported = errors.New("unsupported")

type factoryFunc func(string) models.RevisionController

var controllers = map[string]factoryFunc{}

func register(controllerID string, fn factoryFunc) {
	controllers[controllerID] = fn
}

func New(controllerID string, path string) (models.RevisionController, error) {
	factoryFunc, ok := controllers[controllerID]
	if !ok {
		return nil, errors.New("cannot create revision control instance")
	}
	return factoryFunc(path), nil
}

type detector interface {
	Support() bool
	Root() (string, error)
}

func Detect(path string) (string, string, error) {
	for controllerID, factoryFunc := range controllers {
		rc, ok := factoryFunc(path).(detector)
		if ok && rc.Support() {
			log.Tracef("support found for %v", controllerID)
			root, err := rc.Root()
			if err != nil {
				continue
			}
			log.Tracef("root found in %s", root)
			return controllerID, root, nil
		}
	}
	return "", "", fmt.Errorf("no supported repository found in %s", path)
}