| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package discovery |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/framework/confgroup" |
| 10 | ) |
| 11 | |
| 12 | // Discoverer is a discovery source runner. |
| 13 | type Discoverer interface { |
| 14 | Run(ctx context.Context, in chan<- []*confgroup.Group) |
| 15 | } |
| 16 | |
| 17 | // ProviderFactory builds optional discoverers from a shared build context. |
| 18 | type ProviderFactory interface { |
| 19 | Name() string |
| 20 | Build(ctx BuildContext) (Discoverer, bool, error) |
| 21 | } |
| 22 | |
| 23 | type providerFactoryFunc struct { |
| 24 | name string |
| 25 | build func(ctx BuildContext) (Discoverer, bool, error) |
| 26 | } |
| 27 | |
| 28 | func (p providerFactoryFunc) Name() string { |
| 29 | return p.name |
| 30 | } |
| 31 | |
| 32 | func (p providerFactoryFunc) Build(ctx BuildContext) (Discoverer, bool, error) { |
| 33 | if p.build == nil { |
| 34 | return nil, false, fmt.Errorf("provider %q has nil build function", p.name) |
| 35 | } |
| 36 | return p.build(ctx) |
| 37 | } |
| 38 | |
| 39 | // NewProviderFactory creates a named provider factory. |
| 40 | func NewProviderFactory(name string, build func(ctx BuildContext) (Discoverer, bool, error)) ProviderFactory { |
| 41 | return providerFactoryFunc{name: name, build: build} |
| 42 | } |