From 5ae0e75e5e112a9dd9338d44699cbeaaa13a4370 Mon Sep 17 00:00:00 2001 From: mjarkk Date: Sat, 10 Nov 2018 08:39:09 +0100 Subject: [PATCH] Switched to channels instaid of a mutex --- pkg/commands/exec_live_default.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/pkg/commands/exec_live_default.go b/pkg/commands/exec_live_default.go index 0297cdcd1..9e39585bc 100644 --- a/pkg/commands/exec_live_default.go +++ b/pkg/commands/exec_live_default.go @@ -33,19 +33,7 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s return errorMessage, err } - // canAsk makes sure there are no data races in go - var canAskLock sync.Mutex - canAskValue := true - canAsk := func() bool { - canAskLock.Lock() - defer canAskLock.Unlock() - return canAskValue - } - stopCanAsk := func() { - canAskLock.Lock() - defer canAskLock.Unlock() - canAskValue = false - } + stopAsking := make(chan struct{}) var waitForBufio sync.WaitGroup waitForBufio.Add(1) @@ -57,9 +45,12 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s go func() { scanner := bufio.NewScanner(tty) scanner.Split(scanWordsWithNewLines) + loop: for scanner.Scan() { - // canAsk prefrents calls to output when the program is already closed - if canAsk() { + select { + case <-stopAsking: + break loop + default: toOutput := strings.Trim(scanner.Text(), " ") cmdOutput = append(cmdOutput, toOutput) toWrite := output(toOutput) @@ -72,7 +63,7 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s }() if err = cmd.Wait(); err != nil { - stopCanAsk() + stopAsking <- struct{}{} waitForBufio.Wait() return strings.Join(cmdOutput, " "), err }