Skip to content

Commit

Permalink
Update wrt comment: go routine with unexported function to stream out…
Browse files Browse the repository at this point in the history
…put to stdout and the CommandOutput struct
  • Loading branch information
ashrithagoramane committed Sep 26, 2024
1 parent 1015a05 commit 574555a
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions venv.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func makeVenv(cfg VenvConfig) error {
}
logrus.Debugln("Detected Python version:", pythonVersion)

// venv was introduced in python version 3.3
// venv was introduced in python version 3.3
useVenv := pythonVersionAtLeast(pythonVersion, 3, 3)
logrus.Debugln("Use venv:", useVenv)

Expand Down Expand Up @@ -155,6 +155,22 @@ type VenvCommandRunOutput struct {
Exitcode int
}

func streamOutput(stream io.ReadCloser, outString *string) error {
var buff bytes.Buffer
scanner := bufio.NewScanner(stream)
scanner.Split(bufio.ScanLines)

for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
buff.WriteString(m)
buff.WriteString("\n")
}
*outString = fmt.Sprint(buff.String())

return nil
}

// Run will execute the command described in VenvCommand.
//
// The strings returned are Stdout/Stderr.
Expand Down Expand Up @@ -202,29 +218,9 @@ func (c VenvCommand) Run() VenvCommandRunOutput {
CommandOutput.Error = errors.Wrap(err, "unable to start command")
return CommandOutput
}

output := map[io.ReadCloser]*string{
stdout: &CommandOutput.Stdout,
stderr: &CommandOutput.Stderr,
}

for stream, out := range output {
go func(s io.ReadCloser, o *string) {
var buff bytes.Buffer

scanner := bufio.NewScanner(s)
scanner.Split(bufio.ScanLines)

for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
buff.WriteString(m)
buff.WriteString("\n")
}
*o = fmt.Sprint(buff.String())

}(stream, out)
}
go streamOutput(stdout, &CommandOutput.Stdout)
go streamOutput(stderr, &CommandOutput.Stderr)

if err := cmd.Wait(); err != nil {
exitError, _ := err.(*exec.ExitError)
Expand Down

0 comments on commit 574555a

Please sign in to comment.