Skip to content

Commit

Permalink
adjust byte buffer reading
Browse files Browse the repository at this point in the history
  • Loading branch information
aalu1418 committed Apr 2, 2024
1 parent f5fe2d8 commit d1629f6
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions gauntlet/gauntlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
package gauntlet

import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -73,7 +74,6 @@ type ExecCommandOptions struct {
//
// It will also check for any errors you specify in the output via the errHandling slice.
func (g *Gauntlet) ExecCommand(args []string, options ExecCommandOptions) (string, error) {
output := ""
var updatedArgs []string
if g.Command == "gauntlet" {
updatedArgs = append([]string{g.Command}, args...)
Expand All @@ -93,22 +93,34 @@ func (g *Gauntlet) ExecCommand(args []string, options ExecCommandOptions) (strin
stderr, _ := cmd.StderrPipe()
cmdErr := cmd.Start()

reader := bufio.NewReader(stdout)
line, err := reader.ReadString('\n')
for err == nil {
log.Info().Str("stdout", line).Msg(g.Command)
output = fmt.Sprintf("%s%s", output, line)
line, err = reader.ReadString('\n')
// reader := bufio.NewReader(stdout)
// line, err := reader.ReadString('\n')
// for err == nil {
// log.Info().Str("stdout", line).Msg(g.Command)
// output = fmt.Sprintf("%s%s", output, line)
// line, err = reader.ReadString('\n')
// }

// reader = bufio.NewReader(stderr)
// line, err = reader.ReadString('\n')
// for err == nil {
// log.Info().Str("stderr", line).Msg(g.Command)
// output = fmt.Sprintf("%s%s", output, line)
// line, err = reader.ReadString('\n')
// }

outBytes, _ := io.ReadAll(stdout)
errBytes, _ := io.ReadAll(stderr)

for _, line := range bytes.Split(outBytes, []byte("\n")) {
log.Info().Str("stdout", string(line)).Msg(g.Command)
}

reader = bufio.NewReader(stderr)
line, err = reader.ReadString('\n')
for err == nil {
log.Info().Str("stderr", line).Msg(g.Command)
output = fmt.Sprintf("%s%s", output, line)
line, err = reader.ReadString('\n')
for _, line := range bytes.Split(errBytes, []byte("\n")) {
log.Info().Str("stderr", string(line)).Msg(g.Command)
}

output := string(outBytes) + "\n" + string(errBytes)

// return cmdErr from before, after stdout + stderr logging
if cmdErr != nil {
return output, cmdErr
Expand All @@ -121,12 +133,12 @@ func (g *Gauntlet) ExecCommand(args []string, options ExecCommandOptions) (strin
}
}

if strings.Compare("EOF", err.Error()) > 0 {
return output, err
}
// if strings.Compare("EOF", err.Error()) > 0 {
// return output, err
// }

// catch any exit codes
err = cmd.Wait()
err := cmd.Wait()

log.Debug().Str("Command", g.Command).Msg("command Completed")
return output, err
Expand Down

0 comments on commit d1629f6

Please sign in to comment.