2022-02-13 10:52:19 +01:00
|
|
|
// Copyright 2022 - Offen Authors <hioffen@posteo.de>
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-02-02 21:07:25 +01:00
|
|
|
"errors"
|
2022-02-13 10:52:19 +01:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// hook contains a queued action that can be trigger them when the script
|
|
|
|
// reaches a certain point (e.g. unsuccessful backup)
|
|
|
|
type hook struct {
|
|
|
|
level hookLevel
|
|
|
|
action func(err error) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type hookLevel int
|
|
|
|
|
|
|
|
const (
|
|
|
|
hookLevelPlumbing hookLevel = iota
|
|
|
|
hookLevelError
|
|
|
|
hookLevelInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
var hookLevels = map[string]hookLevel{
|
|
|
|
"info": hookLevelInfo,
|
|
|
|
"error": hookLevelError,
|
|
|
|
}
|
|
|
|
|
|
|
|
// registerHook adds the given action at the given level.
|
|
|
|
func (s *script) registerHook(level hookLevel, action func(err error) error) {
|
|
|
|
s.hooks = append(s.hooks, hook{level, action})
|
|
|
|
}
|
|
|
|
|
|
|
|
// runHooks runs all hooks that have been registered using the
|
|
|
|
// given levels in the defined ordering. In case executing a hook returns an
|
|
|
|
// error, the following hooks will still be run before the function returns.
|
|
|
|
func (s *script) runHooks(err error) error {
|
|
|
|
sort.SliceStable(s.hooks, func(i, j int) bool {
|
|
|
|
return s.hooks[i].level < s.hooks[j].level
|
|
|
|
})
|
|
|
|
var actionErrors []error
|
|
|
|
for _, hook := range s.hooks {
|
|
|
|
if hook.level > s.hookLevel {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if actionErr := hook.action(err); actionErr != nil {
|
|
|
|
actionErrors = append(actionErrors, fmt.Errorf("runHooks: error running hook: %w", actionErr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(actionErrors) != 0 {
|
2023-02-02 21:07:25 +01:00
|
|
|
return errors.Join(actionErrors...)
|
2022-02-13 10:52:19 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|