Skip to content

Commit cbd1b14

Browse files
committed
Changed exit status for all 'twitch event' subcommands to return 0 on success and 1 on error.
1 parent 74cca29 commit cbd1b14

File tree

2 files changed

+64
-49
lines changed

2 files changed

+64
-49
lines changed

cmd/events.go

+47-40
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ var triggerCmd = &cobra.Command{
7373
%s`, types.AllWebhookTopics()),
7474
Args: cobra.MaximumNArgs(1),
7575
ValidArgs: types.AllWebhookTopics(),
76-
Run: triggerCmdRun,
76+
PreRun: silenceBeforeRunE,
77+
RunE: triggerCmdRun,
7778
Example: `twitch event trigger subscribe`,
7879
Aliases: []string{
7980
"fire", "emit",
@@ -88,19 +89,21 @@ var verifyCmd = &cobra.Command{
8889
%s`, types.AllWebhookTopics()),
8990
Args: cobra.MaximumNArgs(1),
9091
ValidArgs: types.AllWebhookTopics(),
91-
Run: verifyCmdRun,
92+
PreRun: silenceBeforeRunE,
93+
RunE: verifyCmdRun,
9294
Example: `twitch event verify-subscription subscribe`,
9395
Aliases: []string{
9496
"verify",
9597
},
9698
}
9799

98100
var websocketCmd = &cobra.Command{
99-
Use: "websocket [action]",
100-
Short: `Executes actions regarding the mock EventSub WebSocket server. See "twitch event websocket --help" for usage info.`,
101-
Long: fmt.Sprintf(`Executes actions regarding the mock EventSub WebSocket server.`),
102-
Args: cobra.MaximumNArgs(1),
103-
Run: websocketCmdRun,
101+
Use: "websocket [action]",
102+
Short: `Executes actions regarding the mock EventSub WebSocket server. See "twitch event websocket --help" for usage info.`,
103+
Long: fmt.Sprintf(`Executes actions regarding the mock EventSub WebSocket server.`),
104+
Args: cobra.MaximumNArgs(1),
105+
PreRun: silenceBeforeRunE,
106+
RunE: websocketCmdRun,
104107
Example: fmt.Sprintf(` twitch event websocket start-server
105108
twitch event websocket reconnect
106109
twitch event websocket close --session=e411cc1e_a2613d4e --reason=4006
@@ -116,7 +119,8 @@ var websocketCmd = &cobra.Command{
116119
var retriggerCmd = &cobra.Command{
117120
Use: "retrigger",
118121
Short: "Refires events based on the event ID. Can be forwarded to the local webserver for event testing.",
119-
Run: retriggerCmdRun,
122+
PreRun: silenceBeforeRunE,
123+
RunE: retriggerCmdRun,
120124
Example: `twitch event retrigger subscribe`,
121125
}
122126

@@ -125,6 +129,12 @@ var startWebsocketServerCmd = &cobra.Command{
125129
Deprecated: `use "twitch event websocket start-server" instead.`,
126130
}
127131

132+
// This is required with commands that use RunE to solve the issues described below
133+
func silenceBeforeRunE(cmd *cobra.Command, args []string) {
134+
cmd.SilenceErrors = true // Prevents printing the help message after an error occurs
135+
cmd.SilenceUsage = true // Prevents printing the error message; This is already done in root.go[cmd.Execute()]
136+
}
137+
128138
func init() {
129139
rootCmd.AddCommand(eventCmd)
130140

@@ -190,28 +200,25 @@ func init() {
190200
websocketCmd.Flags().StringVar(&wsReason, "reason", "", `Sets the close reason when sending a Close message to the client. Used with "websocket close".`)
191201
}
192202

193-
func triggerCmdRun(cmd *cobra.Command, args []string) {
203+
func triggerCmdRun(cmd *cobra.Command, args []string) error {
194204
if len(args) == 0 {
195205
cmd.Help()
196-
return
206+
return fmt.Errorf("")
197207
}
198208

199209
if transport == "websub" {
200-
fmt.Println(websubDeprecationNotice)
201-
return
210+
return fmt.Errorf(websubDeprecationNotice)
202211
}
203212

204213
if secret != "" && (len(secret) < 10 || len(secret) > 100) {
205-
fmt.Println("Invalid secret provided. Secrets must be between 10-100 characters")
206-
return
214+
return fmt.Errorf("Invalid secret provided. Secrets must be between 10-100 characters")
207215
}
208216

209217
// Validate that the forward address is actually a URL
210218
if len(forwardAddress) > 0 {
211219
_, err := url.ParseRequestURI(forwardAddress)
212220
if err != nil {
213-
fmt.Println(err)
214-
return
221+
return err
215222
}
216223
}
217224

@@ -243,23 +250,22 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
243250
})
244251

245252
if err != nil {
246-
println(err.Error())
247-
return
253+
return err
248254
}
249255

250256
fmt.Println(res)
251257
}
258+
259+
return nil
252260
}
253261

254-
func retriggerCmdRun(cmd *cobra.Command, args []string) {
262+
func retriggerCmdRun(cmd *cobra.Command, args []string) error {
255263
if transport == "websub" {
256-
fmt.Println(websubDeprecationNotice)
257-
return
264+
return fmt.Errorf(websubDeprecationNotice)
258265
}
259266

260267
if secret != "" && (len(secret) < 10 || len(secret) > 100) {
261-
fmt.Println("Invalid secret provided. Secrets must be between 10-100 characters")
262-
return
268+
return fmt.Errorf("Invalid secret provided. Secrets must be between 10-100 characters")
263269
}
264270

265271
res, err := trigger.RefireEvent(eventID, trigger.TriggerParameters{
@@ -268,35 +274,32 @@ func retriggerCmdRun(cmd *cobra.Command, args []string) {
268274
Timestamp: util.GetTimestamp().Format(time.RFC3339Nano),
269275
})
270276
if err != nil {
271-
fmt.Printf("Error refiring event: %s", err)
272-
return
277+
return fmt.Errorf("Error refiring event: %s", err)
273278
}
274279

275280
fmt.Println(res)
281+
return nil
276282
}
277283

278-
func verifyCmdRun(cmd *cobra.Command, args []string) {
284+
func verifyCmdRun(cmd *cobra.Command, args []string) error {
279285
if len(args) == 0 {
280286
cmd.Help()
281-
return
287+
return fmt.Errorf("")
282288
}
283289

284290
if transport == "websub" {
285-
fmt.Println(websubDeprecationNotice)
286-
return
291+
return fmt.Errorf(websubDeprecationNotice)
287292
}
288293

289294
if secret != "" && (len(secret) < 10 || len(secret) > 100) {
290-
fmt.Println("Invalid secret provided. Secrets must be between 10-100 characters")
291-
return
295+
return fmt.Errorf("Invalid secret provided. Secrets must be between 10-100 characters")
292296
}
293297

294298
// Validate that the forward address is actually a URL
295299
if len(forwardAddress) > 0 {
296300
_, err := url.ParseRequestURI(forwardAddress)
297301
if err != nil {
298-
fmt.Println(err)
299-
return
302+
return err
300303
}
301304
}
302305

@@ -306,11 +309,10 @@ func verifyCmdRun(cmd *cobra.Command, args []string) {
306309
// Verify custom timestamp
307310
_, err := time.Parse(time.RFC3339Nano, timestamp)
308311
if err != nil {
309-
fmt.Println(
312+
return fmt.Errorf(
310313
`Discarding verify: Invalid timestamp provided.
311314
Please follow RFC3339Nano, which is used by Twitch as seen here:
312315
https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`)
313-
return
314316
}
315317
}
316318

@@ -324,15 +326,16 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
324326
})
325327

326328
if err != nil {
327-
println(err.Error())
328-
return
329+
return err
329330
}
331+
332+
return nil
330333
}
331334

332-
func websocketCmdRun(cmd *cobra.Command, args []string) {
335+
func websocketCmdRun(cmd *cobra.Command, args []string) error {
333336
if len(args) == 0 {
334337
cmd.Help()
335-
return
338+
return fmt.Errorf("")
336339
}
337340

338341
if args[0] == "start-server" || args[0] == "start" {
@@ -341,11 +344,15 @@ func websocketCmdRun(cmd *cobra.Command, args []string) {
341344
mock_server.StartWebsocketServer(wsDebug, wsServerIP, wsServerPort, wsSSL, wsStrict)
342345
} else {
343346
// Forward all other commands via RPC
344-
websocket.ForwardWebsocketCommand(args[0], websocket.WebsocketCommandParameters{
347+
err := websocket.ForwardWebsocketCommand(args[0], websocket.WebsocketCommandParameters{
345348
Client: wsClient,
346349
Subscription: wsSubscription,
347350
SubscriptionStatus: wsStatus,
348351
CloseReason: wsReason,
349352
})
353+
354+
return err
350355
}
356+
357+
return nil
351358
}

internal/events/websocket/websocket_cmd.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,17 @@ type WebsocketCommandParameters struct {
1616
CloseReason string
1717
}
1818

19-
func ForwardWebsocketCommand(cmd string, p WebsocketCommandParameters) {
19+
func ForwardWebsocketCommand(cmd string, p WebsocketCommandParameters) error {
2020
client, err := rpc.DialHTTP("tcp", ":44747")
2121
if err != nil {
22-
println("Failed to dial RPC handler for WebSocket server. Is it online?")
23-
println("Error: " + err.Error())
24-
return
22+
return fmt.Errorf("Failed to dial RPC handler for WebSocket server. Is it online?\nError: %v", err.Error())
2523
}
2624

2725
var reply rpc_handler.RPCResponse
2826

2927
rpcName := mock_server.ResolveRPCName(cmd)
3028
if rpcName == "" {
31-
println("Invalid websocket command")
32-
return
29+
return fmt.Errorf("Invalid websocket command")
3330
}
3431

3532
// Command line flags to be passed with the command
@@ -47,18 +44,29 @@ func ForwardWebsocketCommand(cmd string, p WebsocketCommandParameters) {
4744

4845
err = client.Call("RPCHandler.ExecuteGenericRPC", args, &reply)
4946

47+
if err != nil {
48+
return fmt.Errorf("Failed to call RPC method RPCHandler.ExecuteGenericRPC: %v", err.Error())
49+
}
50+
5051
switch reply.ResponseCode {
5152
case mock_server.COMMAND_RESPONSE_SUCCESS:
5253
color.New().Add(color.FgGreen).Println(fmt.Sprintf("✔ Forwarded for use in mock EventSub WebSocket server"))
54+
return nil
5355

5456
case mock_server.COMMAND_RESPONSE_FAILED_ON_SERVER:
55-
color.New().Add(color.FgRed).Println(fmt.Sprintf("✗ EventSub WebSocket server failed to process command:\n%v", reply.DetailedInfo))
57+
return fmt.Errorf(
58+
color.New().Add(color.FgRed).Sprintln(fmt.Sprintf("✗ EventSub WebSocket server failed to process command:\n%v", reply.DetailedInfo)),
59+
)
5660

5761
case mock_server.COMMAND_RESPONSE_MISSING_FLAG:
58-
color.New().Add(color.FgRed).Println(fmt.Sprintf("✗ Command rejected for invalid flags:\n%v", reply.DetailedInfo))
62+
return fmt.Errorf(
63+
color.New().Add(color.FgRed).Sprintln(fmt.Sprintf("✗ Command rejected for invalid flags:\n%v", reply.DetailedInfo)),
64+
)
5965

6066
case mock_server.COMMAND_RESPONSE_INVALID_CMD:
61-
println("Invalid websocket sub-command: " + cmd)
67+
return fmt.Errorf("Invalid websocket sub-command: %v", cmd)
6268

6369
}
70+
71+
return fmt.Errorf("RPCHandler experienced unexpected response code: %v", reply.ResponseCode)
6472
}

0 commit comments

Comments
 (0)