-
Notifications
You must be signed in to change notification settings - Fork 149
/
trigger.go
141 lines (130 loc) · 4.03 KB
/
trigger.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cmd
import (
"errors"
"fmt"
"strings"
"text/tabwriter"
"github.com/argoproj/notifications-engine/pkg/triggers"
"github.com/argoproj/notifications-engine/pkg/util/misc"
"github.com/spf13/cobra"
)
func newTriggerCommand(cmdContext *commandContext) *cobra.Command {
var command = cobra.Command{
Use: "trigger",
Short: "Notification triggers related commands",
RunE: func(c *cobra.Command, args []string) error {
return errors.New("select child command")
},
}
command.AddCommand(newTriggerRunCommand(cmdContext))
command.AddCommand(newTriggerGetCommand(cmdContext))
return &command
}
func newTriggerRunCommand(cmdContext *commandContext) *cobra.Command {
var command = cobra.Command{
Use: "run NAME RESOURCE_NAME",
Short: "Evaluates specified trigger condition and prints the result",
Example: fmt.Sprintf(`
# Execute trigger configured in 'argocd-notification-cm' ConfigMap
%s trigger run on-sync-status-unknown ./sample-app.yaml
# Execute trigger using my-config-map.yaml instead of '%s' ConfigMap
%s trigger run on-sync-status-unknown ./sample-app.yaml \
--config-map ./my-config-map.yaml`, cmdContext.cliName, cmdContext.ConfigMapName, cmdContext.cliName),
RunE: func(c *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("expected two arguments, got %d", len(args))
}
name := args[0]
resourceName := args[1]
api, err := cmdContext.getAPI()
if err != nil {
_, _ = fmt.Fprintf(cmdContext.stderr, "failed to get api: %v\n", err)
return nil
}
_, ok := api.GetConfig().Triggers[name]
if !ok {
var names []string
for name := range api.GetConfig().Triggers {
names = append(names, name)
}
_, _ = fmt.Fprintf(cmdContext.stderr,
"trigger with name '%s' does not exist (found %s)\n", name, strings.Join(names, ", "))
return nil
}
r, err := cmdContext.loadResource(resourceName)
if err != nil {
_, _ = fmt.Fprintf(cmdContext.stderr, "failed to load resource: %v\n", err)
return nil
}
res, err := api.RunTrigger(name, r.Object)
if err != nil {
_, _ = fmt.Fprintf(cmdContext.stderr, "failed to execute trigger %s: %v\n", name, err)
return nil
}
w := tabwriter.NewWriter(cmdContext.stdout, 5, 0, 2, ' ', 0)
_, _ = fmt.Fprintf(w, "CONDITION\tRESULT\n")
for i := range res {
_, _ = fmt.Fprintf(w, "%s\t%v\n", api.GetConfig().Triggers[name][i].When, res[i].Triggered)
}
_ = w.Flush()
return nil
},
}
return &command
}
func newTriggerGetCommand(cmdContext *commandContext) *cobra.Command {
var (
output string
)
var command = cobra.Command{
Use: "get",
Example: fmt.Sprintf(`
# prints all triggers
%s trigger get
# print YAML formatted on-sync-failed trigger definition
%s trigger get on-sync-failed -o=yaml
`, cmdContext.cliName, cmdContext.cliName),
Short: "Prints information about configured triggers",
RunE: func(c *cobra.Command, args []string) error {
var name string
if len(args) == 1 {
name = args[0]
}
items := map[string][]triggers.Condition{}
api, err := cmdContext.getAPI()
if err != nil {
_, _ = fmt.Fprintf(cmdContext.stderr, "failed to get api: %v\n", err)
return nil
}
for triggerName, trigger := range api.GetConfig().Triggers {
if triggerName == name || name == "" {
items[triggerName] = trigger
}
}
switch output {
case "", "wide":
w := tabwriter.NewWriter(cmdContext.stdout, 5, 0, 2, ' ', 0)
_, _ = fmt.Fprintf(w, "NAME\tTEMPLATE\tCONDITION\n")
misc.IterateStringKeyMap(items, func(triggerName string) {
for i, condition := range items[triggerName] {
name := triggerName
if i > 0 {
name = ""
}
_, _ = fmt.Fprintf(w, "%s\t%v\t%s\n", name, strings.Join(condition.Send, ", "), condition.When)
}
})
_ = w.Flush()
case "name":
misc.IterateStringKeyMap(items, func(name string) {
_, _ = fmt.Println(name)
})
default:
return misc.PrintFormatted(items, output, cmdContext.stdout)
}
return nil
},
}
addOutputFlags(&command, &output)
return &command
}