nurse/check/registry.go
Peter Kurfer 9791e9f282
All checks were successful
Renovate / renovate (push) Successful in 40s
Go build / build (push) Successful in 8m44s
feat: refactor to server and exec-check subcommands
- allow to interactively execute checks instead of server mode
- use urfave/cli for subcommands
2023-12-04 11:22:49 +01:00

57 lines
1,013 B
Go

package check
import (
"errors"
"fmt"
"strings"
"sync"
)
var (
ErrModuleNameConflict = errors.New("module name conflict")
ErrNoSuchModule = errors.New("no module of given name known")
)
func NewRegistry() *Registry {
return &Registry{
mods: make(map[string]*Module),
}
}
type (
Registry struct {
lock sync.RWMutex
mods map[string]*Module
}
)
func (r *Registry) Register(modules ...*Module) error {
r.lock.Lock()
defer r.lock.Unlock()
for _, mod := range modules {
modName := strings.ToLower(mod.Name())
if _, ok := r.mods[modName]; ok {
return fmt.Errorf("%w: %s", ErrModuleNameConflict, modName)
}
r.mods[modName] = mod
}
return nil
}
//nolint:ireturn // required to implement interface
func (r *Registry) Lookup(modName string) (CheckerLookup, error) {
r.lock.RLock()
defer r.lock.RUnlock()
modName = strings.ToLower(modName)
if mod, ok := r.mods[modName]; !ok {
return nil, fmt.Errorf("%w: %s", ErrNoSuchModule, modName)
} else {
return mod, nil
}
}