-
Notifications
You must be signed in to change notification settings - Fork 22
/
control_hooks_status.go
77 lines (61 loc) · 1.71 KB
/
control_hooks_status.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
package controlstatus
import (
"context"
"fmt"
"github.com/spf13/viper"
"github.com/turbot/pipe-fittings/constants"
"github.com/turbot/pipe-fittings/statushooks"
"github.com/turbot/pipe-fittings/utils"
)
// StatusControlHooks is a struct which implements ControlHooks, and displays the control progress as a status message
type StatusControlHooks struct {
Enabled bool
}
func NewStatusControlHooks() *StatusControlHooks {
return &StatusControlHooks{
Enabled: viper.GetBool(constants.ArgProgress),
}
}
func (c *StatusControlHooks) OnStart(ctx context.Context, _ *ControlProgress) {
if !c.Enabled {
return
}
statushooks.SetStatus(ctx, "Starting controls…")
statushooks.Show(ctx)
}
func (c *StatusControlHooks) OnControlStart(ctx context.Context, _ ControlRunStatusProvider, p *ControlProgress) {
if !c.Enabled {
return
}
c.setStatusFromProgress(ctx, p)
}
func (c *StatusControlHooks) OnControlComplete(ctx context.Context, _ ControlRunStatusProvider, p *ControlProgress) {
if !c.Enabled {
return
}
c.setStatusFromProgress(ctx, p)
}
func (c *StatusControlHooks) OnControlError(ctx context.Context, _ ControlRunStatusProvider, p *ControlProgress) {
if !c.Enabled {
return
}
c.setStatusFromProgress(ctx, p)
}
func (c *StatusControlHooks) OnComplete(ctx context.Context, _ *ControlProgress) {
if !c.Enabled {
return
}
statushooks.Done(ctx)
}
func (c *StatusControlHooks) setStatusFromProgress(ctx context.Context, p *ControlProgress) {
message := fmt.Sprintf("Running %d %s. (%d complete, %d running, %d pending, %d %s)",
p.Total,
utils.Pluralize("control", p.Total),
p.Complete,
p.Executing,
p.Pending,
p.Error,
utils.Pluralize("error", p.Error),
)
statushooks.SetStatus(ctx, message)
}