From cf1e9f79b1d1f7faad5c2ccff4536936b10f3e19 Mon Sep 17 00:00:00 2001 From: mjarkk Date: Sat, 3 Nov 2018 09:36:38 +0100 Subject: [PATCH] hopefully fixed the test now --- pkg/commands/exec_live_default.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/commands/exec_live_default.go b/pkg/commands/exec_live_default.go index 88bbf2eb4..27363c3b9 100644 --- a/pkg/commands/exec_live_default.go +++ b/pkg/commands/exec_live_default.go @@ -20,7 +20,6 @@ import ( // NOTE: You don't have to include a enter in the return data this function will do that for you func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) (errorMessage string, codeError error) { cmdOutput := []string{} - canAsk := true splitCmd := ToArgv(command) cmd := exec.Command(splitCmd[0], splitCmd[1:]...) @@ -34,6 +33,19 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s return errorMessage, err } + 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 + } + var waitForBufio sync.WaitGroup waitForBufio.Add(1) @@ -50,7 +62,7 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s scanner.Split(bufio.ScanWords) for scanner.Scan() { // canAsk prefrents calls to output when the program is already closed - if canAsk { + if canAsk() { toOutput := re.ReplaceAllString(scanner.Text(), "") cmdOutput = append(cmdOutput, toOutput) toWrite := output(toOutput) @@ -63,9 +75,7 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s }() if err := cmd.Wait(); err != nil { - canAsk = false - - // + stopCanAsk() waitForBufio.Wait() return strings.Join(cmdOutput, " "), err }