feedkit: split the former maximumdirect.net/weatherd project in two.
feedkit now contains a reusable core, while weatherfeeder is a concrete implementation that includes weather-specific functions.
This commit is contained in:
33
sinks/registry.go
Normal file
33
sinks/registry.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package sinks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
)
|
||||
|
||||
// Factory constructs a sink instance from config.
|
||||
//
|
||||
// This is the mechanism that lets concrete daemons wire in whatever sinks they
|
||||
// want without main.go being full of switch statements.
|
||||
type Factory func(cfg config.SinkConfig) (Sink, error)
|
||||
|
||||
type Registry struct {
|
||||
byDriver map[string]Factory
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{byDriver: map[string]Factory{}}
|
||||
}
|
||||
|
||||
func (r *Registry) Register(driver string, f Factory) {
|
||||
r.byDriver[driver] = f
|
||||
}
|
||||
|
||||
func (r *Registry) Build(cfg config.SinkConfig) (Sink, error) {
|
||||
f, ok := r.byDriver[cfg.Driver]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown sink driver: %q", cfg.Driver)
|
||||
}
|
||||
return f(cfg)
|
||||
}
|
||||
Reference in New Issue
Block a user