Skip to content

Commit 3b8a9f5

Browse files
committed
Try to use Peek for peeking progress if available
Signed-off-by: Andrea Waltlova <[email protected]>
1 parent ce4ef02 commit 3b8a9f5

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/runner.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,32 @@ func runCommandWithOutput(cmd *exec.Cmd, outputCh chan string, doneCh chan bool)
114114
defer close(dataReadCh)
115115

116116
go func() {
117-
defer func() {
118-
dataReadCh <- true
119-
}()
120-
121117
reader := bufio.NewReader(cmdOutput)
118+
readBuffer := 1024
122119

123120
for {
124-
line, err := reader.ReadString('\n')
125-
if errors.Is(err, io.EOF) {
121+
data, err := reader.Peek(readBuffer)
122+
switch {
123+
case errors.Is(err, io.EOF):
126124
log.Infoln("Read ended with EOF")
127-
break
128-
} else if err != nil {
129-
log.Infoln("Read ended with error", err)
130-
outputCh <- fmt.Sprintf("Error reading from stdout: %v", err)
125+
outputCh <- string(data)
126+
dataReadCh <- true
131127
return
128+
case errors.Is(err, io.ErrShortBuffer):
129+
log.Infoln("Shorter read than wanted", err, data)
130+
if len(data) != 0 {
131+
log.Infoln("Not empty", data)
132+
outputCh <- string(data)
133+
_, err := reader.Discard(len(data))
134+
if err != nil {
135+
// TODO: what should I do if I want to move the reader
136+
// If I do nothing it can only cause it to be read twice
137+
log.Errorln("Discard failed", err)
138+
}
139+
}
140+
default:
141+
log.Infoln(data, err)
132142
}
133-
log.Infoln("Read line: ", line)
134-
outputCh <- line
135143
}
136144
}()
137145

@@ -174,18 +182,15 @@ func executeCommandWithProgress(command string, interpreter string, variables ma
174182
for {
175183
select {
176184
case output := <-outputCh:
177-
// TODO: this has to be sent to dispatcher back to report to UI
178-
// the idea is to send partial output if buffer with given size sent the output to channel
179-
log.Info(output)
180-
181-
// Append partial to all output
182185
bufferedOutput += output
183186
case <-ticker.C:
184187
// NOTE: If just message without output is also okay we could send just still running
185188
log.Infoln("Still running ...")
189+
log.Infoln(bufferedOutput)
186190
case <-doneCh:
187191
// Execution is done
188192
log.Infoln("Execution done ...")
193+
log.Infoln(bufferedOutput)
189194
return bufferedOutput
190195
}
191196
}

0 commit comments

Comments
 (0)