File: revctrl.go

package info (click to toggle)
aerc 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,900 kB
  • sloc: ansic: 1,181; python: 1,000; sh: 553; awk: 360; makefile: 23
file content (48 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download | duplicates (2)
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/log"
	"git.sr.ht/~rjarry/aerc/lib/pama/models"
)

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)
}