diff options
author | Reto Brunner <reto@labrat.space> | 2019-07-18 06:25:42 +0200 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-07-19 15:37:55 -0400 |
commit | 6fed04bb9fa43d3886d47d0c845d32fff11569b5 (patch) | |
tree | 461c8086c06236761555f08a614f88d7c058523b | |
parent | 66a9052f0fa8d2caf0d82435241b10e9ba8665b2 (diff) | |
download | aerc-6fed04bb9fa43d3886d47d0c845d32fff11569b5.tar.gz |
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.
-rw-r--r-- | worker/handlers/register.go | 27 | ||||
-rw-r--r-- | worker/imap/worker.go | 10 | ||||
-rw-r--r-- | worker/maildir/worker.go | 7 | ||||
-rw-r--r-- | worker/worker.go | 24 | ||||
-rw-r--r-- | worker/worker_enabled.go | 5 |
5 files changed, 53 insertions, 20 deletions
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 +} diff --git a/worker/imap/worker.go b/worker/imap/worker.go index de869941..b1ef9c91 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -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 { diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go index 1e68a2e6..84e0162d 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -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) diff --git a/worker/worker.go b/worker/worker.go index a37927e7..f1f47c38 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -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 } diff --git a/worker/worker_enabled.go b/worker/worker_enabled.go new file mode 100644 index 00000000..4fba7297 --- /dev/null +++ b/worker/worker_enabled.go @@ -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" |