hooks: adapt to new per-interface features

Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
Steffen Vogel
2022-10-01 11:23:29 +02:00
parent 817a79af24
commit e74a8acb95
3 changed files with 28 additions and 15 deletions

View File

@@ -26,7 +26,7 @@ type ExecHook struct {
logger *zap.Logger
}
func (h *Interface) NewExecHook(cfg *config.ExecHookSetting) {
func (h *Interface) NewExecHook(cfg *config.ExecHookSetting) *ExecHook {
hk := &ExecHook{
ExecHookSetting: cfg,
logger: h.logger.Named("exec").With(
@@ -36,7 +36,7 @@ func (h *Interface) NewExecHook(cfg *config.ExecHookSetting) {
h.logger.Debug("Created new exec hook", zap.Any("hook", hk))
h.registerHook(hk)
return hk
}
func (h *ExecHook) run(msg proto.Message, args ...any) {

View File

@@ -24,6 +24,8 @@ type Hook interface {
type Interface struct {
*daemon.Interface
hooks []Hook
logger *zap.Logger
}
@@ -37,31 +39,42 @@ func New(i *daemon.Interface) (daemon.Feature, error) {
logger: zap.L().Named("hooks").With(zap.String("intf", i.Name())),
}
for _, hk := range i.Settings.Hooks {
switch hk := hk.(type) {
for _, hks := range i.Settings.Hooks {
var hk Hook
switch hks := hks.(type) {
case *config.ExecHookSetting:
h.NewExecHook(hk)
hk = h.NewExecHook(hks)
case *config.WebHookSetting:
h.NewWebHook(hk)
hk = h.NewWebHook(hks)
}
h.OnModified(hk)
h.OnPeer(hk)
if ep, ok := h.Features["epdisc"]; ok {
ep.(*epdisc.Interface).OnConnectionStateChange(hk)
}
h.hooks = append(h.hooks, hk)
}
return h, nil
}
func (h *Interface) registerHook(j Hook) {
h.Daemon.Watcher.OnAll(j)
ep := h.Features["epdisc"].(*epdisc.Interface)
ep.OnConnectionStateChange(j)
}
func (h *Interface) Start() error {
h.logger.Info("Started hooks")
for _, hk := range h.hooks {
hk.OnInterfaceAdded(h.Interface.Interface)
}
return nil
}
func (h *Interface) Close() error {
for _, hk := range h.hooks {
hk.OnInterfaceRemoved(h.Interface.Interface)
}
return nil
}

View File

@@ -28,7 +28,7 @@ type WebHook struct {
logger *zap.Logger
}
func (h *Interface) NewWebHook(cfg *config.WebHookSetting) {
func (h *Interface) NewWebHook(cfg *config.WebHookSetting) *WebHook {
hk := &WebHook{
WebHookSetting: cfg,
logger: h.logger.Named("web").With(
@@ -38,7 +38,7 @@ func (h *Interface) NewWebHook(cfg *config.WebHookSetting) {
h.logger.Debug("Created new web hook", zap.Any("hook", hk))
h.registerHook(hk)
return hk
}
func (h *WebHook) run(msg proto.Message) {