Skip to content

Commit 9f6739f

Browse files
twz123banjoh
andauthored
feat(support-bundle): print progress in debug non-interactive mode (#1010)
Currently, there's no debug logs whatsoever when running a command like troubleshoot --debug --interactive=false Tackle this by printing similar log statements as presented in interactive mode to the debug logger. Refactor the code a bit so there's no need for a dedicated finished channel and to exit the goroutines properly. Co-authored-by: Evans Mungai <[email protected]>
1 parent 661a948 commit 9f6739f

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/signal"
1010
"path/filepath"
1111
"strings"
12+
"sync"
1213
"time"
1314

1415
cursor "github.com/ahmetalpbalkan/go-cursor"
@@ -208,44 +209,49 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
208209
}
209210
additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, redactors...)
210211

211-
var collectorCB func(chan interface{}, string)
212-
progressChan := make(chan interface{}) // non-zero buffer can result in missed messages
213-
finishedCh := make(chan bool, 1)
214-
isFinishedChClosed := false
212+
var wg sync.WaitGroup
213+
collectorCB := func(c chan interface{}, msg string) { c <- msg }
214+
progressChan := make(chan interface{})
215+
isProgressChanClosed := false
216+
defer func() {
217+
if !isProgressChanClosed {
218+
close(progressChan)
219+
}
220+
wg.Wait()
221+
}()
215222

216223
if !interactive {
217224
// TODO (dans): custom warning handler to capture warning in `analysisOutput`
218225
restConfig.WarningHandler = rest.NoWarnings{}
219-
collectorCB = func(ch chan interface{}, name string) {
220-
return
221-
}
222226

223227
// TODO (dans): maybe log to file
228+
wg.Add(1)
224229
go func() {
225-
for {
226-
select {
227-
case _ = <-progressChan:
228-
// do nothing
229-
}
230+
defer wg.Done()
231+
for msg := range progressChan {
232+
logger.Printf("Collecting support bundle: %v", msg)
230233
}
231234
}()
232235
} else {
233236
s := spin.New()
237+
wg.Add(1)
234238
go func() {
239+
defer wg.Done()
235240
currentDir := ""
236241
for {
237242
select {
238-
case msg := <-progressChan:
243+
case msg, ok := <-progressChan:
244+
if !ok {
245+
fmt.Printf("\r%s\r", cursor.ClearEntireLine())
246+
return
247+
}
239248
switch msg := msg.(type) {
240249
case error:
241250
c := color.New(color.FgHiRed)
242251
c.Println(fmt.Sprintf("%s\r * %v", cursor.ClearEntireLine(), msg))
243252
case string:
244253
currentDir = filepath.Base(msg)
245254
}
246-
case <-finishedCh:
247-
fmt.Printf("\r%s\r", cursor.ClearEntireLine())
248-
return
249255
case <-time.After(time.Millisecond * 100):
250256
if currentDir == "" {
251257
fmt.Printf("\r%s \033[36mCollecting support bundle\033[m %s", cursor.ClearEntireLine(), s.Next())
@@ -255,16 +261,6 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
255261
}
256262
}
257263
}()
258-
defer func() {
259-
if !isFinishedChClosed {
260-
close(finishedCh)
261-
}
262-
}()
263-
264-
collectorCB = func(c chan interface{}, msg string) {
265-
c <- fmt.Sprintf("%s", msg)
266-
}
267-
268264
}
269265

270266
createOpts := supportbundle.SupportBundleCreateOpts{
@@ -290,11 +286,12 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
290286
if err != nil {
291287
return errors.Wrap(err, "failed to run collect and analyze process")
292288
}
289+
290+
close(progressChan) // this removes the spinner in interactive mode
291+
isProgressChanClosed = true
292+
293293
if len(response.AnalyzerResults) > 0 {
294294
if interactive {
295-
close(finishedCh) // this removes the spinner
296-
isFinishedChClosed = true
297-
298295
if err := showInteractiveResults(mainBundle.Name, response.AnalyzerResults); err != nil {
299296
interactive = false
300297
}

0 commit comments

Comments
 (0)