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.
This commit is contained in:
Reto Brunner 2019-07-18 06:25:42 +02:00 committed by Drew DeVault
parent 66a9052f0f
commit 6fed04bb9f
5 changed files with 53 additions and 20 deletions

View file

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

View file

@ -13,9 +13,15 @@ import (
"git.sr.ht/~sircmpwn/aerc/lib"
"git.sr.ht/~sircmpwn/aerc/models"
"git.sr.ht/~sircmpwn/aerc/worker/handlers"
"git.sr.ht/~sircmpwn/aerc/worker/types"
)
func init() {
handlers.RegisterWorkerFactory("imap", NewIMAPWorker)
handlers.RegisterWorkerFactory("imaps", NewIMAPWorker)
}
var errUnsupported = fmt.Errorf("unsupported command")
type imapClient struct {
@ -43,12 +49,12 @@ type IMAPWorker struct {
seqMap []uint32
}
func NewIMAPWorker(worker *types.Worker) *IMAPWorker {
func NewIMAPWorker(worker *types.Worker) (types.Backend, error) {
return &IMAPWorker{
idleDone: make(chan error),
updates: make(chan client.Update, 50),
worker: worker,
}
}, nil
}
func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {

View file

@ -11,9 +11,14 @@ import (
"github.com/fsnotify/fsnotify"
"git.sr.ht/~sircmpwn/aerc/models"
"git.sr.ht/~sircmpwn/aerc/worker/handlers"
"git.sr.ht/~sircmpwn/aerc/worker/types"
)
func init() {
handlers.RegisterWorkerFactory("maildir", NewWorker)
}
var errUnsupported = fmt.Errorf("unsupported command")
// A Worker handles interfacing between aerc's UI and a group of maildirs.
@ -25,7 +30,7 @@ type Worker struct {
}
// NewWorker creates a new maildir worker with the provided worker.
func NewWorker(worker *types.Worker) (*Worker, error) {
func NewWorker(worker *types.Worker) (types.Backend, error) {
watch, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("could not create file system watcher: %v", err)

View file

@ -1,14 +1,13 @@
package worker
import (
"git.sr.ht/~sircmpwn/aerc/worker/imap"
"git.sr.ht/~sircmpwn/aerc/worker/maildir"
"git.sr.ht/~sircmpwn/aerc/worker/types"
"fmt"
"log"
"net/url"
"strings"
"git.sr.ht/~sircmpwn/aerc/worker/handlers"
"git.sr.ht/~sircmpwn/aerc/worker/types"
)
// Guesses the appropriate worker type based on the given source string
@ -23,19 +22,10 @@ func NewWorker(source string, logger *log.Logger) (*types.Worker, error) {
scheme = scheme[:strings.IndexRune(scheme, '+')]
fmt.Println(scheme)
}
switch scheme {
case "imap":
fallthrough
case "imaps":
worker.Backend = imap.NewIMAPWorker(worker)
case "maildir":
if w, err := maildir.NewWorker(worker); err != nil {
return nil, fmt.Errorf("could not create maildir worker: %v", err)
} else {
worker.Backend = w
}
default:
return nil, fmt.Errorf("Unknown backend %s", u.Scheme)
backend, err := handlers.GetHandlerForScheme(scheme, worker)
if err != nil {
return nil, err
}
worker.Backend = backend
return worker, nil
}

5
worker/worker_enabled.go Normal file
View file

@ -0,0 +1,5 @@
package worker
// the following workers are always enabled
import _ "git.sr.ht/~sircmpwn/aerc/worker/imap"
import _ "git.sr.ht/~sircmpwn/aerc/worker/maildir"