Skip to content

Commit

Permalink
Update eachmodule to stream output when not running concurrently (#893)
Browse files Browse the repository at this point in the history
Updates the eachmodule utility to stream the invoked command output and
error to stdout when the utility is run without concurrency. This allows
better viewing of the command output being invoked. When invoked with
concurrency, the utility falls back to the old behavior of buffering the
output.
  • Loading branch information
jasdel authored Nov 13, 2020
1 parent 9f8c2ec commit 95c006a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
13 changes: 8 additions & 5 deletions internal/repotools/cmd/eachmodule/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ type Work struct {
type WorkLog struct {
Path, Cmd string
Err error
Output *bytes.Buffer
Output io.Reader
}

// CommandWorker provides a consumer of work jobs and posts results to the worklog
func CommandWorker(ctx context.Context, jobs <-chan Work, results chan<- WorkLog) {
func CommandWorker(ctx context.Context, jobs <-chan Work, results chan<- WorkLog, outWriter io.ReadWriter) {
for {
var result WorkLog

Expand All @@ -35,12 +35,15 @@ func CommandWorker(ctx context.Context, jobs <-chan Work, results chan<- WorkLog
if !ok {
return
}
output := bytes.NewBuffer(nil)
if outWriter == nil {
outWriter = bytes.NewBuffer(nil)
result.Output = outWriter
}

result.Path = w.Path
result.Cmd = w.Cmd
result.Output = output

cmd, err := NewCommand(ctx, output, output, w.Path, w.Cmd)
cmd, err := NewCommand(ctx, outWriter, outWriter, w.Path, w.Cmd)
if err != nil {
result.Err = fmt.Errorf("failed to build command, %w", err)
break
Expand Down
22 changes: 19 additions & 3 deletions internal/repotools/cmd/eachmodule/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -135,13 +137,23 @@ func run() (err error) {
relPath = result.Path
}

var output string
if result.Output != nil {
b, err := ioutil.ReadAll(result.Output)
if err != nil {
log.Printf("%s: %s => failed to read result output, %v",
relPath, result.Cmd, err)
}
output = string(b)
}

if result.Err != nil {
log.Printf("%s: %s => error: %v\n%s",
relPath, result.Cmd, result.Err, result.Output.String())
relPath, result.Cmd, result.Err, output)
failed = true
} else {
log.Printf("%s: %s =>\n%s",
relPath, result.Cmd, result.Output.String())
relPath, result.Cmd, output)
}

// Terminate early as soon as any command fails.
Expand All @@ -158,7 +170,11 @@ func run() (err error) {
for i := 0; i < atOnce; i++ {
go func() {
defer jobWG.Done()
CommandWorker(ctx, jobs, results)
var output io.ReadWriter
if atOnce == 1 {
output = os.Stdout
}
CommandWorker(ctx, jobs, results, output)
}()
}

Expand Down

0 comments on commit 95c006a

Please sign in to comment.