aboutsummaryrefslogblamecommitdiffstats
path: root/lib/pama/revctrl/revctrl.go
blob: cd9202f07a2905d239c3ffe971fc277f1a47883a (plain) (tree)
1
2
3
4
5
6
7
8





                
                                        
                                                







































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