-
Notifications
You must be signed in to change notification settings - Fork 22
/
context.go
52 lines (41 loc) · 1.5 KB
/
context.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
package controlstatus
import (
"context"
"github.com/turbot/pipe-fittings/contexthelpers"
)
var (
contextKeyControlHook = contexthelpers.ContextKey("control_hook")
)
func AddControlHooksToContext(ctx context.Context, statusHooks ControlHooks) context.Context {
// if the context already contains ControlHooks, do nothing
// this may happen when executing a dashboard snapshot -
if _, ok := ctx.Value(contextKeyControlHook).(ControlHooks); ok {
return ctx
}
return context.WithValue(ctx, contextKeyControlHook, statusHooks)
}
func ControlHooksFromContext(ctx context.Context) ControlHooks {
if ctx == nil {
return NullHooks
}
if val, ok := ctx.Value(contextKeyControlHook).(ControlHooks); ok {
return val
}
// no status hook in context - return null status hook
return NullHooks
}
func OnStart(ctx context.Context, p *ControlProgress) {
ControlHooksFromContext(ctx).OnStart(ctx, p)
}
func OnControlStart(ctx context.Context, controlRun ControlRunStatusProvider, p *ControlProgress) {
ControlHooksFromContext(ctx).OnControlStart(ctx, controlRun, p)
}
func OnControlComplete(ctx context.Context, controlRun ControlRunStatusProvider, p *ControlProgress) {
ControlHooksFromContext(ctx).OnControlComplete(ctx, controlRun, p)
}
func OnControlError(ctx context.Context, controlRun ControlRunStatusProvider, p *ControlProgress) {
ControlHooksFromContext(ctx).OnControlError(ctx, controlRun, p)
}
func OnComplete(ctx context.Context, p *ControlProgress) {
ControlHooksFromContext(ctx).OnComplete(ctx, p)
}