This repository was archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathhandler.go
79 lines (64 loc) · 1.86 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package handler
import (
"context"
"sort"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
"github.com/aquasecurity/fanal/analyzer"
"github.com/aquasecurity/fanal/artifact"
"github.com/aquasecurity/fanal/types"
)
var (
postHandlerInits = map[types.HandlerType]postHandlerInit{}
)
type postHandlerInit func(artifact.Option) (PostHandler, error)
type PostHandler interface {
Type() types.HandlerType
Version() int
Handle(context.Context, *analyzer.AnalysisResult, *types.BlobInfo) error
Priority() int
}
// RegisterPostHandlerInit adds a constructor of post handler
func RegisterPostHandlerInit(t types.HandlerType, init postHandlerInit) {
postHandlerInits[t] = init
}
func DeregisterPostHandler(t types.HandlerType) {
delete(postHandlerInits, t)
}
type Manager struct {
postHandlers []PostHandler
}
func NewManager(artifactOpt artifact.Option) (Manager, error) {
var m Manager
for t, handlerInit := range postHandlerInits {
// Skip the handler if it is disabled
if slices.Contains(artifactOpt.DisabledHandlers, t) {
continue
}
handler, err := handlerInit(artifactOpt)
if err != nil {
return Manager{}, xerrors.Errorf("post handler %s initialize error: %w", t, err)
}
m.postHandlers = append(m.postHandlers, handler)
}
// Sort post handlers by priority
sort.Slice(m.postHandlers, func(i, j int) bool {
return m.postHandlers[i].Priority() > m.postHandlers[j].Priority()
})
return m, nil
}
func (m Manager) Versions() map[string]int {
versions := map[string]int{}
for _, h := range m.postHandlers {
versions[string(h.Type())] = h.Version()
}
return versions
}
func (m Manager) PostHandle(ctx context.Context, result *analyzer.AnalysisResult, blob *types.BlobInfo) error {
for _, h := range m.postHandlers {
if err := h.Handle(ctx, result, blob); err != nil {
return xerrors.Errorf("post handler error: %w", err)
}
}
return nil
}