Skip to content

Commit

Permalink
Preserve pipewriter with newline introduction
Browse files Browse the repository at this point in the history
  • Loading branch information
RobGeada committed Nov 11, 2024
1 parent f06c428 commit e648080
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions controllers/lmes/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,27 @@ func (dc *driverComm) notifyShutdownWait() {
dc.connection <- 1
}

type FormattedWriter struct{}
type PodLogAndPipeWriter struct {
pw *io.PipeWriter
}

func (f FormattedWriter) Write(p []byte) (n int, err error) {
// format lm-eval output to stdout
func (f PodLogAndPipeWriter) Write(p []byte) (n int, err error) {
// format lm-eval output to stdout and the pipewriter
line := string(p)
res, err := fmt.Print(line)
fmt.Print(line)
if _, err := f.pw.Write(p); err != nil {
return 0, err
}

// carriage returns do not correctly display, so replace with newlines
// carriage returns also break the pipewriter, so make sure we get newlines as well.
if strings.ContainsAny(line, "\r") && !strings.ContainsAny(line, "\n") {
fmt.Print("\n")
if _, err := f.pw.Write([]byte("\n")); err != nil {
return 0, err
}
}
return res, err
return 0, nil
}

func (d *driverImpl) exec() error {
Expand Down Expand Up @@ -330,8 +339,8 @@ func (d *driverImpl) exec() error {

// have a pipe to check the output and report progress
// lm-eval's outputs are in the stderr
pr, _ := io.Pipe()
mwriter := io.MultiWriter(stderr, FormattedWriter{})
pr, pw := io.Pipe()
mwriter := io.MultiWriter(stderr, PodLogAndPipeWriter{pw})
scanner := bufio.NewScanner(pr)

executor := exec.Command(d.Option.Args[0], args...)
Expand Down

0 comments on commit e648080

Please sign in to comment.