Files
cunicu/pkg/daemon/feature/hooks/hooks.go
Steffen Vogel a8f67be889 ci: use golangci-lint for linting
Signed-off-by: Steffen Vogel <post@steffenvogel.de>
2022-12-07 23:12:05 +01:00

79 lines
1.4 KiB
Go

package hooks
import (
"github.com/stv0g/cunicu/pkg/config"
"github.com/stv0g/cunicu/pkg/core"
"github.com/stv0g/cunicu/pkg/daemon"
"github.com/stv0g/cunicu/pkg/daemon/feature/epdisc"
"go.uber.org/zap"
)
func init() { //nolint:gochecknoinits
daemon.RegisterFeature("hooks", "Hooks", New, 70)
}
type Hook interface {
core.AllHandler
epdisc.OnConnectionStateHandler
}
type Interface struct {
*daemon.Interface
hooks []Hook
logger *zap.Logger
}
func New(i *daemon.Interface) (daemon.Feature, error) {
if len(i.Settings.Hooks) == 0 {
return nil, nil
}
h := &Interface{
Interface: i,
logger: zap.L().Named("hooks").With(zap.String("intf", i.Name())),
}
for _, hks := range i.Settings.Hooks {
var hk Hook
switch hks := hks.(type) {
case *config.ExecHookSetting:
hk = h.NewExecHook(hks)
case *config.WebHookSetting:
hk = h.NewWebHook(hks)
}
h.OnModified(hk)
h.OnPeer(hk)
if f, ok := h.Features["epdisc"]; ok {
if ep, ok := f.(*epdisc.Interface); ok {
ep.OnConnectionStateChange(hk)
}
}
h.hooks = append(h.hooks, hk)
}
return h, nil
}
func (i *Interface) Start() error {
i.logger.Info("Started hooks")
for _, hk := range i.hooks {
hk.OnInterfaceAdded(i.Interface.Interface)
}
return nil
}
func (i *Interface) Close() error {
for _, hk := range i.hooks {
hk.OnInterfaceRemoved(i.Interface.Interface)
}
return nil
}