-
Notifications
You must be signed in to change notification settings - Fork 290
/
action.go
112 lines (95 loc) · 3.64 KB
/
action.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package execute
import (
"bytes"
"context"
"fmt"
"text/tabwriter"
"github.com/sirupsen/logrus"
"github.com/kubeshop/botkube/pkg/bot/interactive"
"github.com/kubeshop/botkube/pkg/config"
"github.com/kubeshop/botkube/pkg/execute/command"
"github.com/kubeshop/botkube/pkg/maputil"
)
const (
actionNameMissing = "You forgot to pass action name. Please pass one of the following valid actions:\n\n%s"
actionEnabled = "I have enabled '%s' action on '%s' cluster."
actionDisabled = "Done. I won't run '%s' action on '%s' cluster."
)
var (
actionFeatureName = FeatureName{
Name: "action",
Aliases: []string{"actions", "act"},
}
)
type ActionsStorage interface {
PersistActionEnabled(ctx context.Context, name string, enabled bool) error
}
// ActionExecutor executes all commands that are related to actions.
type ActionExecutor struct {
log logrus.FieldLogger
cfgManager ActionsStorage
actions map[string]config.Action
}
// NewActionExecutor returns a new ActionExecutor instance.
func NewActionExecutor(log logrus.FieldLogger, cfgManager ActionsStorage, cfg config.Config) *ActionExecutor {
return &ActionExecutor{
log: log,
cfgManager: cfgManager,
actions: cfg.Actions,
}
}
// Commands returns slice of commands the executor supports
func (e *ActionExecutor) Commands() map[command.Verb]CommandFn {
return map[command.Verb]CommandFn{
command.ListVerb: e.List,
command.EnableVerb: e.Enable,
command.DisableVerb: e.Disable,
}
}
// FeatureName returns the name and aliases of the feature provided by this executor
func (e *ActionExecutor) FeatureName() FeatureName {
return actionFeatureName
}
// List returns a tabular representation of Actions
func (e *ActionExecutor) List(ctx context.Context, cmdCtx CommandContext) (interactive.CoreMessage, error) {
e.log.Debug("List actions")
return respond(e.ActionsTabularOutput(), cmdCtx), nil
}
// Enable enables given action in the runtime config map
func (e *ActionExecutor) Enable(ctx context.Context, cmdCtx CommandContext) (interactive.CoreMessage, error) {
const enabled = true
if len(cmdCtx.Args) < 3 {
return respond(fmt.Sprintf(actionNameMissing, e.ActionsTabularOutput()), cmdCtx), nil
}
actionName := cmdCtx.Args[2]
e.log.Debug("Enabling action...", actionName)
if err := e.cfgManager.PersistActionEnabled(ctx, actionName, enabled); err != nil {
return interactive.CoreMessage{}, fmt.Errorf("while setting action %q to %t: %w", actionName, enabled, err)
}
return respond(fmt.Sprintf(actionEnabled, actionName, cmdCtx.ClusterName), cmdCtx), nil
}
// Disable disables given action in the runtime config map
func (e *ActionExecutor) Disable(ctx context.Context, cmdCtx CommandContext) (interactive.CoreMessage, error) {
const enabled = false
if len(cmdCtx.Args) < 3 {
return respond(fmt.Sprintf(actionNameMissing, e.ActionsTabularOutput()), cmdCtx), nil
}
actionName := cmdCtx.Args[2]
e.log.Debug("Disabling action...", actionName)
if err := e.cfgManager.PersistActionEnabled(ctx, actionName, enabled); err != nil {
return interactive.CoreMessage{}, fmt.Errorf("while setting action %q to %t: %w", actionName, enabled, err)
}
return respond(fmt.Sprintf(actionDisabled, actionName, cmdCtx.ClusterName), cmdCtx), nil
}
// ActionsTabularOutput sorts actions by key and returns a printable table
func (e *ActionExecutor) ActionsTabularOutput() string {
keys := maputil.SortKeys(e.actions)
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 5, 0, 1, ' ', 0)
fmt.Fprintf(w, "ACTION\tENABLED \tDISPLAY NAME")
for _, name := range keys {
fmt.Fprintf(w, "\n%s\t%v \t%s", name, e.actions[name].Enabled, e.actions[name].DisplayName)
}
w.Flush()
return buf.String()
}