Skip to content

Commit

Permalink
fix stream output
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelsq committed Oct 29, 2020
1 parent 057bfc8 commit 813eade
Showing 1 changed file with 47 additions and 54 deletions.
101 changes: 47 additions & 54 deletions pkg/wtc/wtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -532,87 +533,76 @@ func trig(rule *Rule, pkg, path string) error {
return nil
}

func pipeChar(tpe, id string, isStderr bool) (*os.File, error) {
ww, rr, err := pty.Open()
if err != nil {
return nil, err
}
func pipeChar(rr io.Reader, tpe, id string, isStderr bool) {

reader := bufio.NewReader(rr)
go func() {
defer rr.Close()

me := false
me := false

cancel := make(chan struct{})
cancel := make(chan struct{})

for {
r, _, err := reader.ReadRune()

if !me {
acquire <- struct{}{}
me = true
go func() {
<-cancel
}()
}
for {
r, _, err := reader.ReadRune()

cancel <- struct{}{}
if !me {
acquire <- struct{}{}
me = true
go func() {
<-cancel
}()
}

if err != nil {
(&Rune{Type: tpe, Title: id, End: true, IsStderr: isStderr}).Log()
me = false
<-acquire
return
}
cancel <- struct{}{}

if r == BR {
(&Rune{Type: tpe, Title: id, Rune: r, IsStderr: isStderr}).Log()
me = false
<-acquire
continue
}
if err != nil {
(&Rune{Type: tpe, Title: id, End: true, IsStderr: isStderr}).Log()
me = false
<-acquire
return
}

if r == BR {
(&Rune{Type: tpe, Title: id, Rune: r, IsStderr: isStderr}).Log()

go func() {
select {
case <-cancel:
case <-time.After(time.Second / 2):
me = false
<-acquire
}
}()
me = false
<-acquire
continue
}
}()

return ww, nil
(&Rune{Type: tpe, Title: id, Rune: r, IsStderr: isStderr}).Log()

go func() {
select {
case <-cancel:
case <-time.After(time.Second / 2):
me = false
<-acquire
}
}()
}
}

func run(ctx context.Context, name, command string, env []string) error {
cmd := exec.Command("sh", "-c", command)

stdout, err := pipeChar(TypeCommandOK, name, false)
stdout, tty, err := pty.Open()
if err != nil {
return err
}
cmd.Stdin = stdout
cmd.Stdout = stdout
defer stdout.Close()
defer tty.Close()

stderr, _ := pipeChar(TypeCommandErr, name, true)
stderr, tty2, err := pty.Open()
if err != nil {
return err
}
cmd.Stderr = stderr
defer stderr.Close()
defer tty2.Close()

cmd.Stdin = os.Stdin
cmd.Stdout = tty
cmd.Stderr = tty2

cmd.Env = env

cmd.SysProcAttr = &syscall.SysProcAttr{
Setctty: true,
Setsid: true,
}
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

if err := cmd.Start(); err != nil {
return err
Expand All @@ -631,6 +621,9 @@ func run(ctx context.Context, name, command string, env []string) error {
close(exit)
}()

go pipeChar(stdout, TypeCommandOK, name, false)
go pipeChar(stderr, TypeCommandErr, name, true)

err = cmd.Wait()
if err != nil && uint32(cmd.ProcessState.Sys().(syscall.WaitStatus)) == uint32(syscall.SIGKILL) {
err = context.Canceled
Expand Down

0 comments on commit 813eade

Please sign in to comment.