Skip to content

Commit

Permalink
tbw: stream stdout/stderr separately
Browse files Browse the repository at this point in the history
unused on client side yet

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
  • Loading branch information
bradfitz committed Oct 22, 2023
1 parent 63857fd commit 8b012cb
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions tbw/tbw.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"path"
"path/filepath"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -100,15 +101,38 @@ func handle(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hi from fly machines.\n")
}

type webWriter struct {
typ int // 1=stdout, 2=stderr, -1, exit status
mu *sync.Mutex
je *json.Encoder
f http.Flusher
}

func (w webWriter) Write(p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
err = w.je.Encode([]any{w.typ, string(p)})
if err != nil {
return 0, err
}
w.f.Flush()
return len(p), nil
}

func test(w http.ResponseWriter, r *http.Request) {
pkg := strings.TrimPrefix(r.RequestURI, "/test/")
log.Printf("testing %v ...", pkg)
t0 := time.Now()
cmd := exec.Command(filepath.Join(codeDir, "tool/go"), "test", "-json", "-v")

var mu sync.Mutex
je := json.NewEncoder(w)
f := w.(http.Flusher)

cmd.Args = append(cmd.Args, pkg)
cmd.Dir = codeDir
cmd.Stdout = w
cmd.Stderr = w
cmd.Stdout = &webWriter{1, &mu, je, f}
cmd.Stderr = &webWriter{2, &mu, je, f}
cmd.Env = append(os.Environ(),
"HOME="+workDir,
)
Expand Down

0 comments on commit 8b012cb

Please sign in to comment.