@@ -73,7 +73,8 @@ var triggerCmd = &cobra.Command{
73
73
%s` , types .AllWebhookTopics ()),
74
74
Args : cobra .MaximumNArgs (1 ),
75
75
ValidArgs : types .AllWebhookTopics (),
76
- Run : triggerCmdRun ,
76
+ PreRun : silenceBeforeRunE ,
77
+ RunE : triggerCmdRun ,
77
78
Example : `twitch event trigger subscribe` ,
78
79
Aliases : []string {
79
80
"fire" , "emit" ,
@@ -88,19 +89,21 @@ var verifyCmd = &cobra.Command{
88
89
%s` , types .AllWebhookTopics ()),
89
90
Args : cobra .MaximumNArgs (1 ),
90
91
ValidArgs : types .AllWebhookTopics (),
91
- Run : verifyCmdRun ,
92
+ PreRun : silenceBeforeRunE ,
93
+ RunE : verifyCmdRun ,
92
94
Example : `twitch event verify-subscription subscribe` ,
93
95
Aliases : []string {
94
96
"verify" ,
95
97
},
96
98
}
97
99
98
100
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 ,
104
107
Example : fmt .Sprintf (` twitch event websocket start-server
105
108
twitch event websocket reconnect
106
109
twitch event websocket close --session=e411cc1e_a2613d4e --reason=4006
@@ -116,7 +119,8 @@ var websocketCmd = &cobra.Command{
116
119
var retriggerCmd = & cobra.Command {
117
120
Use : "retrigger" ,
118
121
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 ,
120
124
Example : `twitch event retrigger subscribe` ,
121
125
}
122
126
@@ -125,6 +129,12 @@ var startWebsocketServerCmd = &cobra.Command{
125
129
Deprecated : `use "twitch event websocket start-server" instead.` ,
126
130
}
127
131
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
+
128
138
func init () {
129
139
rootCmd .AddCommand (eventCmd )
130
140
@@ -190,28 +200,25 @@ func init() {
190
200
websocketCmd .Flags ().StringVar (& wsReason , "reason" , "" , `Sets the close reason when sending a Close message to the client. Used with "websocket close".` )
191
201
}
192
202
193
- func triggerCmdRun (cmd * cobra.Command , args []string ) {
203
+ func triggerCmdRun (cmd * cobra.Command , args []string ) error {
194
204
if len (args ) == 0 {
195
205
cmd .Help ()
196
- return
206
+ return fmt . Errorf ( "" )
197
207
}
198
208
199
209
if transport == "websub" {
200
- fmt .Println (websubDeprecationNotice )
201
- return
210
+ return fmt .Errorf (websubDeprecationNotice )
202
211
}
203
212
204
213
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" )
207
215
}
208
216
209
217
// Validate that the forward address is actually a URL
210
218
if len (forwardAddress ) > 0 {
211
219
_ , err := url .ParseRequestURI (forwardAddress )
212
220
if err != nil {
213
- fmt .Println (err )
214
- return
221
+ return err
215
222
}
216
223
}
217
224
@@ -243,23 +250,22 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
243
250
})
244
251
245
252
if err != nil {
246
- println (err .Error ())
247
- return
253
+ return err
248
254
}
249
255
250
256
fmt .Println (res )
251
257
}
258
+
259
+ return nil
252
260
}
253
261
254
- func retriggerCmdRun (cmd * cobra.Command , args []string ) {
262
+ func retriggerCmdRun (cmd * cobra.Command , args []string ) error {
255
263
if transport == "websub" {
256
- fmt .Println (websubDeprecationNotice )
257
- return
264
+ return fmt .Errorf (websubDeprecationNotice )
258
265
}
259
266
260
267
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" )
263
269
}
264
270
265
271
res , err := trigger .RefireEvent (eventID , trigger.TriggerParameters {
@@ -268,35 +274,32 @@ func retriggerCmdRun(cmd *cobra.Command, args []string) {
268
274
Timestamp : util .GetTimestamp ().Format (time .RFC3339Nano ),
269
275
})
270
276
if err != nil {
271
- fmt .Printf ("Error refiring event: %s" , err )
272
- return
277
+ return fmt .Errorf ("Error refiring event: %s" , err )
273
278
}
274
279
275
280
fmt .Println (res )
281
+ return nil
276
282
}
277
283
278
- func verifyCmdRun (cmd * cobra.Command , args []string ) {
284
+ func verifyCmdRun (cmd * cobra.Command , args []string ) error {
279
285
if len (args ) == 0 {
280
286
cmd .Help ()
281
- return
287
+ return fmt . Errorf ( "" )
282
288
}
283
289
284
290
if transport == "websub" {
285
- fmt .Println (websubDeprecationNotice )
286
- return
291
+ return fmt .Errorf (websubDeprecationNotice )
287
292
}
288
293
289
294
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" )
292
296
}
293
297
294
298
// Validate that the forward address is actually a URL
295
299
if len (forwardAddress ) > 0 {
296
300
_ , err := url .ParseRequestURI (forwardAddress )
297
301
if err != nil {
298
- fmt .Println (err )
299
- return
302
+ return err
300
303
}
301
304
}
302
305
@@ -306,11 +309,10 @@ func verifyCmdRun(cmd *cobra.Command, args []string) {
306
309
// Verify custom timestamp
307
310
_ , err := time .Parse (time .RFC3339Nano , timestamp )
308
311
if err != nil {
309
- fmt .Println (
312
+ return fmt .Errorf (
310
313
`Discarding verify: Invalid timestamp provided.
311
314
Please follow RFC3339Nano, which is used by Twitch as seen here:
312
315
https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event` )
313
- return
314
316
}
315
317
}
316
318
@@ -324,15 +326,16 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
324
326
})
325
327
326
328
if err != nil {
327
- println (err .Error ())
328
- return
329
+ return err
329
330
}
331
+
332
+ return nil
330
333
}
331
334
332
- func websocketCmdRun (cmd * cobra.Command , args []string ) {
335
+ func websocketCmdRun (cmd * cobra.Command , args []string ) error {
333
336
if len (args ) == 0 {
334
337
cmd .Help ()
335
- return
338
+ return fmt . Errorf ( "" )
336
339
}
337
340
338
341
if args [0 ] == "start-server" || args [0 ] == "start" {
@@ -341,11 +344,15 @@ func websocketCmdRun(cmd *cobra.Command, args []string) {
341
344
mock_server .StartWebsocketServer (wsDebug , wsServerIP , wsServerPort , wsSSL , wsStrict )
342
345
} else {
343
346
// Forward all other commands via RPC
344
- websocket .ForwardWebsocketCommand (args [0 ], websocket.WebsocketCommandParameters {
347
+ err := websocket .ForwardWebsocketCommand (args [0 ], websocket.WebsocketCommandParameters {
345
348
Client : wsClient ,
346
349
Subscription : wsSubscription ,
347
350
SubscriptionStatus : wsStatus ,
348
351
CloseReason : wsReason ,
349
352
})
353
+
354
+ return err
350
355
}
356
+
357
+ return nil
351
358
}
0 commit comments