Refactor commands into separating Start() and Wait()
This commit broke more than I'd like to admit
This commit is contained in:
parent
7b971eea37
commit
3626d5ff1b
2
main.go
2
main.go
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue