@@ -58,28 +58,38 @@ var (
5858)
5959
6060func runFlowCapture (_ * cobra.Command , _ []string ) {
61- go scanner ()
61+ go func () {
62+ if ! scanner () {
63+ return
64+ }
65+ // scanner returns on exit request
66+ os .Exit (0 )
67+ }()
6268
6369 captureType = "Flow"
6470 wg := sync.WaitGroup {}
6571 wg .Add (len (ports ))
6672 for i := range ports {
6773 go func (idx int ) {
6874 defer wg .Done ()
69- runFlowCaptureOnAddr (ports [idx ], nodes [idx ])
75+ err := runFlowCaptureOnAddr (ports [idx ], nodes [idx ])
76+ if err != nil {
77+ // Only fatal errors are returned here
78+ log .Fatal (err )
79+ }
7080 }(i )
7181 }
7282 wg .Wait ()
7383}
7484
75- func runFlowCaptureOnAddr (port int , filename string ) {
85+ func runFlowCaptureOnAddr (port int , filename string ) error {
7686 if len (filename ) > 0 {
7787 log .Infof ("Starting Flow Capture for %s..." , filename )
7888 } else {
7989 log .Infof ("Starting Flow Capture..." )
80- filename = strings .Replace (
90+ filename = strings .ReplaceAll (
8191 currentTime ().UTC ().Format (time .RFC3339 ),
82- ":" , "" , - 1 ) // get rid of offensive colons
92+ ":" , "" ) // get rid of offensive colons
8393 }
8494
8595 var f * os.File
@@ -105,8 +115,7 @@ func runFlowCaptureOnAddr(port int, filename string) {
105115 flowPackets := make (chan * genericmap.Flow , 100 )
106116 collector , err := grpc .StartCollector (port , flowPackets )
107117 if err != nil {
108- log .Error ("StartCollector failed:" , err .Error ())
109- log .Fatal (err )
118+ return fmt .Errorf ("StartCollector failed: %w" , err )
110119 }
111120 log .Trace ("Started collector" )
112121 collectorStarted = true
@@ -128,7 +137,7 @@ func runFlowCaptureOnAddr(port int, filename string) {
128137
129138 if stopReceived {
130139 log .Trace ("Stop received" )
131- return
140+ return nil
132141 }
133142 // parse and display flow async
134143 go parseGenericMapAndDisplay (fp .GenericMap .Value )
@@ -145,18 +154,18 @@ func runFlowCaptureOnAddr(port int, filename string) {
145154 // append new line between each record to read file easilly
146155 bytes , err := f .Write (append (fp .GenericMap .Value , []byte (",\n " )... ))
147156 if err != nil {
148- log . Fatal ( err )
157+ return err
149158 }
150159 if ! captureStarted {
151160 log .Trace ("Wrote flows to json" )
152161 }
153162
154163 // terminate capture if max bytes reached
155- totalBytes = totalBytes + int64 (bytes )
164+ totalBytes += int64 (bytes )
156165 if totalBytes > maxBytes {
157166 if exit := onLimitReached (); exit {
158167 log .Infof ("Capture reached %s, exiting now..." , sizestr .ToString (maxBytes ))
159- return
168+ return nil
160169 }
161170 }
162171
@@ -166,12 +175,13 @@ func runFlowCaptureOnAddr(port int, filename string) {
166175 if int (duration ) > int (maxTime ) {
167176 if exit := onLimitReached (); exit {
168177 log .Infof ("Capture reached %s, exiting now..." , maxTime )
169- return
178+ return nil
170179 }
171180 }
172181
173182 captureStarted = true
174183 }
184+ return nil
175185}
176186
177187func parseGenericMapAndDisplay (bytes []byte ) {
@@ -202,7 +212,7 @@ func manageFlowsDisplay(genericMap config.GenericMap) {
202212 if len (regexes ) > 0 {
203213 // regexes may change during the render so we make a copy first
204214 rCopy := make ([]string , len (regexes ))
205- copy (rCopy [:] , regexes )
215+ copy (rCopy , regexes )
206216 filtered := []config.GenericMap {}
207217 for _ , flow := range lastFlows {
208218 match := true
@@ -380,10 +390,11 @@ func cycleOption(selection []string, exclusiveOptions []string, options []string
380390 return selection
381391}
382392
383- func scanner () {
393+ // scanner returns true in case of normal exit (end of program execution) or false in case of error
394+ func scanner () bool {
384395 if err := keyboard .Open (); err != nil {
385396 keyboardError = fmt .Sprintf ("Keyboard not supported %v" , err )
386- return
397+ return false
387398 }
388399 defer func () {
389400 _ = keyboard .Close ()
@@ -394,26 +405,26 @@ func scanner() {
394405 if err != nil {
395406 panic (err )
396407 }
397- if key == keyboard .KeyCtrlC || stopReceived {
408+ switch {
409+ case key == keyboard .KeyCtrlC , stopReceived :
398410 log .Info ("Ctrl-C pressed, exiting program." )
399-
400411 // exit program
401- os . Exit ( 0 )
402- } else if key == keyboard .KeyArrowUp {
403- flowsToShow = flowsToShow + 1
404- } else if key == keyboard .KeyArrowDown {
412+ return true
413+ case key == keyboard .KeyArrowUp :
414+ flowsToShow ++
415+ case key == keyboard .KeyArrowDown :
405416 if flowsToShow > 10 {
406- flowsToShow = flowsToShow - 1
417+ flowsToShow --
407418 }
408- } else if key == keyboard .KeyArrowRight {
419+ case key == keyboard .KeyArrowRight :
409420 display = cycleOption (display , exclusiveDisplays , displays , 1 )
410- } else if key == keyboard .KeyArrowLeft {
421+ case key == keyboard .KeyArrowLeft :
411422 display = cycleOption (display , exclusiveDisplays , displays , - 1 )
412- } else if key == keyboard .KeyPgup {
423+ case key == keyboard .KeyPgup :
413424 enrichment = cycleOption (enrichment , exclusiveEnrichments , enrichments , 1 )
414- } else if key == keyboard .KeyPgdn {
425+ case key == keyboard .KeyPgdn :
415426 enrichment = cycleOption (enrichment , exclusiveEnrichments , enrichments , - 1 )
416- } else if key == keyboard .KeyBackspace || key == keyboard .KeyBackspace2 {
427+ case key == keyboard .KeyBackspace || key == keyboard .KeyBackspace2 :
417428 if len (regexes ) > 0 {
418429 lastIndex := len (regexes ) - 1
419430 if len (regexes [lastIndex ]) > 0 {
@@ -422,14 +433,14 @@ func scanner() {
422433 regexes = regexes [:lastIndex ]
423434 }
424435 }
425- } else if key == keyboard .KeyEnter {
436+ case key == keyboard .KeyEnter :
426437 regexes = append (regexes , "" )
427- } else {
438+ default :
428439 if len (regexes ) == 0 {
429440 regexes = []string {string (char )}
430441 } else {
431442 lastIndex := len (regexes ) - 1
432- regexes [lastIndex ] = regexes [ lastIndex ] + string (char )
443+ regexes [lastIndex ] += string (char )
433444 }
434445 }
435446 lastRefresh = startupTime
0 commit comments