1
0
Fork 0

Refactor commands into separating Start() and Wait()

This commit broke more than I'd like to admit
This commit is contained in:
Sebastian Schulze 2022-03-04 21:49:46 +01:00
parent 7b971eea37
commit 3626d5ff1b
Signed by: bascht
GPG Key ID: 5BCB1D3B4D38A35A
4 changed files with 18 additions and 23 deletions

View File

@ -108,13 +108,11 @@ func main() {
c.Set("Transfer-Encoding", "chunked")
id := c.Params("id")
fmt.Println("Searching for " + id)
document := documents[id]
c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
fmt.Fprintf(w, "event: status\ndata: started\n\n")
w.Flush()
fmt.Println("WRITER")
for event := range document.Events {
fmt.Fprintf(w, "data: Message: %s\n\n", event)

View File

@ -13,16 +13,8 @@ type Command struct {
cmd *exec.Cmd
}
func (command *Command) Start() {
func (command *Command) Launch() *bufio.Scanner {
command.cmd = exec.Command(command.Name, command.Args...)
command.cmd.Start()
}
func (command *Command) Wait() {
command.cmd.Wait()
}
func (command *Command) NewScanner() *bufio.Scanner {
stderr, err := command.cmd.StderrPipe()
if err != nil {
@ -33,10 +25,17 @@ func (command *Command) NewScanner() *bufio.Scanner {
if err != nil {
log.Fatalf("could not get stdout pipe: %v", err)
}
merged := io.MultiReader(stderr, stdout)
merged := io.MultiReader(stdout, stderr)
// command.cmd.Start()
return bufio.NewScanner(merged)
}
func (command *Command) Wait() {
command.cmd.Wait()
}
func (command *Command) RanSuccessful() bool {
return command.cmd.ProcessState.ExitCode() == 0
}

View File

@ -8,8 +8,7 @@ import (
func TestCommandRun(t *testing.T) {
command := Command{Name: "echo", Args: []string{"Here", "be", "a", "test"}}
scanner := command.NewScanner()
command.Start()
scanner := command.Launch()
for scanner.Scan() {
text := scanner.Text()
if text != "Here be a test" {
@ -20,8 +19,7 @@ func TestCommandRun(t *testing.T) {
func TestCommandOutput(t *testing.T) {
command := Command{Name: "go", Args: []string{"version"}}
scanner := command.NewScanner()
command.Start()
scanner := command.Launch()
for scanner.Scan() {
text := scanner.Text()
if !strings.HasPrefix(text, "go version") {
@ -39,9 +37,7 @@ func TestCommandOutput(t *testing.T) {
func TestCommandFailure(t *testing.T) {
command := Command{Name: "go", Args: []string{"gahdned"}}
scanner := command.NewScanner()
command.Start()
scanner := command.Launch()
var text []string
for scanner.Scan() {
text = append(text, scanner.Text())

View File

@ -30,19 +30,21 @@ func Process(basedir string, document *Document) string {
for _, command := range commands {
done := make(chan bool)
command.Start()
scanner := command.Launch()
go func() {
document.Events <- command.Name + " Startet"
scanner := command.NewScanner()
for scanner.Scan() {
document.Events <- scanner.Text()
}
document.Events <- command.Name + " Ist fast fertig"
done <- true
document.Events <- command.Name + " Ist gestartet"
}()
document.Events <- command.Name + " Ist gestartet"
command.cmd.Start()
command.Wait()
<-done
}