input.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package input
  2. import (
  3. "fmt"
  4. "context"
  5. "go-common/library/log"
  6. "go-common/app/service/ops/log-agent/event"
  7. )
  8. type Input interface {
  9. Run() (err error)
  10. Stop()
  11. Ctx() (ctx context.Context)
  12. }
  13. // Factory is used to register functions creating new Input instances.
  14. type Factory = func(ctx context.Context, config interface{}, connector chan<- *event.ProcessorEvent) (Input, error)
  15. var registry = make(map[string]Factory)
  16. func Register(name string, factory Factory) error {
  17. log.Info("Registering input factory")
  18. if name == "" {
  19. return fmt.Errorf("Error registering input: name cannot be empty")
  20. }
  21. if factory == nil {
  22. return fmt.Errorf("Error registering input '%v': factory cannot be empty", name)
  23. }
  24. if _, exists := registry[name]; exists {
  25. return fmt.Errorf("Error registering input '%v': already registered", name)
  26. }
  27. registry[name] = factory
  28. log.Info("Successfully registered input")
  29. return nil
  30. }
  31. func GetFactory(name string) (Factory, error) {
  32. if _, exists := registry[name]; !exists {
  33. return nil, fmt.Errorf("Error creating input. No such input type exist: '%v'", name)
  34. }
  35. return registry[name], nil
  36. }