Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 12c8813

Browse files
add file flag to send-event to specify file containing multiple events (#336)
Signed-off-by: Nikhil Sharma <nikhilsharma230303@gmail.com>
1 parent 6f128a2 commit 12c8813

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

cmd/sendevent/sendevent.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"os"
2324
"strings"
2425

2526
cloudevents "github.com/cloudevents/sdk-go/v2"
@@ -50,24 +51,42 @@ func NewCmd(config *config.Config, manifest *manifest.Manifest, crd map[string]c
5051
Config: config,
5152
Manifest: manifest,
5253
}
53-
var eventType, target string
54+
var eventType, target, file string
5455
sendCmd := &cobra.Command{
55-
Use: "send-event [--eventType <type>][--target <name>] <data>",
56+
Use: "send-event [--eventType <type>][--target <name>][--file <filename>] <data>",
5657
Short: "Send CloudEvent to the target",
5758
Example: "tmctl send-event '{\"hello\":\"world\"}'",
5859
ValidArgsFunction: func(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
59-
return []string{"--target", "--eventType"}, cobra.ShellCompDirectiveNoFileComp
60+
return []string{"--target", "--eventType", "--file"}, cobra.ShellCompDirectiveNoFileComp
6061
},
6162
RunE: func(cmd *cobra.Command, args []string) error {
6263
cobra.CheckErr(o.Manifest.Read())
6364
if target == "" {
6465
target = o.Config.Context
6566
}
67+
68+
if file != "" {
69+
events, err := readEventsFromFile(file)
70+
if err != nil {
71+
return fmt.Errorf("reading events from file: %w", err)
72+
}
73+
74+
for _, event := range events {
75+
err := o.send(eventType, target, event)
76+
if err != nil {
77+
fmt.Printf("Failed to send event: %v\n", err)
78+
}
79+
}
80+
81+
return nil
82+
}
83+
6684
return o.send(eventType, target, strings.Join(args, " "))
6785
},
6886
}
6987
sendCmd.Flags().StringVar(&target, "target", "", "Component to send the event to. Default is the broker")
7088
sendCmd.Flags().StringVar(&eventType, "eventType", defaultEventType, "CloudEvent Type attribute")
89+
sendCmd.Flags().StringVarP(&file, "file", "f", "", "File containing a list of events")
7190

7291
cobra.CheckErr(sendCmd.RegisterFlagCompletionFunc("eventType", func(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
7392
return completion.ListFilteredEventTypes(o.Config.Context, o.Config.ConfigHome, o.Manifest), cobra.ShellCompDirectiveNoFileComp
@@ -119,3 +138,24 @@ func (o *CliOptions) send(eventType, target, data string) error {
119138
fmt.Printf("\nResponse: %s\n", response)
120139
return nil
121140
}
141+
142+
func readEventsFromFile(file string) ([]string, error) {
143+
var rawEvents []json.RawMessage
144+
145+
fileData, err := os.ReadFile(file)
146+
if err != nil {
147+
return nil, fmt.Errorf("reading file: %w", err)
148+
}
149+
150+
err = json.Unmarshal(fileData, &rawEvents)
151+
if err != nil {
152+
return nil, fmt.Errorf("parsing events from file: %w", err)
153+
}
154+
155+
events := make([]string, len(rawEvents))
156+
for i, rawEvent := range rawEvents {
157+
events[i] = string(rawEvent)
158+
}
159+
160+
return events, nil
161+
}

docs/tmctl_send-event.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Send CloudEvent to the target
44

55
```
6-
tmctl send-event [--eventType <type>][--target <name>] <data> [flags]
6+
tmctl send-event [--eventType <type>][--target <name>][--file <filename>] <data> [flags]
77
```
88

99
### Examples
@@ -16,6 +16,7 @@ tmctl send-event '{"hello":"world"}'
1616

1717
```
1818
--eventType string CloudEvent Type attribute (default "triggermesh-local-event")
19+
-f, --file string File containing a list of events
1920
-h, --help help for send-event
2021
--target string Component to send the event to. Default is the broker
2122
```

0 commit comments

Comments
 (0)