From 6fed04bb9fa43d3886d47d0c845d32fff11569b5 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Thu, 18 Jul 2019 06:25:42 +0200 Subject: Register worker in init. This allows backends which can't always be compiled due to missing dependencies (say libnotmuch) to be compiled conditionally with buildflags. --- worker/handlers/register.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 worker/handlers/register.go (limited to 'worker/handlers/register.go') diff --git a/worker/handlers/register.go b/worker/handlers/register.go new file mode 100644 index 00000000..5e8463e8 --- /dev/null +++ b/worker/handlers/register.go @@ -0,0 +1,27 @@ +package handlers + +import ( + "fmt" + + "git.sr.ht/~sircmpwn/aerc/worker/types" +) + +type FactoryFunc func(*types.Worker) (types.Backend, error) + +var workerFactories map[string]FactoryFunc = make(map[string]FactoryFunc) + +func RegisterWorkerFactory(scheme string, factory FactoryFunc) { + workerFactories[scheme] = factory +} + +func GetHandlerForScheme(scheme string, worker *types.Worker) (types.Backend, error) { + factory, ok := workerFactories[scheme] + if !ok { + return nil, fmt.Errorf("Unknown backend %s", scheme) + } + backend, err := factory(worker) + if err != nil { + return nil, err + } + return backend, nil +} -- cgit