Exemplo n.º 1
0
func Example() {
	var output [2]string

	cmds := []*exec.Cmd{
		exec.Command("sh", "-c", "echo stderr 1>&2"),
		exec.Command("sh", "-c", "echo stdout"),
	}
	closed := 0
	lines, started, exited := cmdplx.Start(cmds)

	for {
		select {
		case line := <-lines:
			if line == nil {
				lines = nil
				if closed++; closed == 3 {
					goto DONE
				}
				break
			}
			if err := line.Err(); err != nil {
				if err != io.EOF {
					fmt.Println(err)
				}
				break
			}
			output[line.From()-1] = line.Text()
		case status := <-started:
			if status == nil {
				started = nil
				if closed++; closed == 3 {
					goto DONE
				}
				break
			}
			if err := status.Err(); err != nil {
				fmt.Println(err)
			}
		case status := <-exited:
			if status == nil {
				exited = nil
				if closed++; closed == 3 {
					goto DONE
				}
				break
			}
			if err := status.Err(); err != nil {
				fmt.Println(err)
			}
		}
	}
DONE:
	fmt.Println(output)
	// Output:
	// [stdout stderr]
}
Exemplo n.º 2
0
func Example_simple() {
	var output [2]string
	lines, _, _ := cmdplx.Start([]*exec.Cmd{
		exec.Command("sh", "-c", "echo stderr 1>&2"),
		exec.Command("sh", "-c", "echo stdout"),
	})

	for line := range lines {
		if err := line.Err(); err == nil {
			output[line.From()-1] = line.Text()
		}
	}

	fmt.Println(output)
	// Output:
	// [stdout stderr]
}
Exemplo n.º 3
0
func TestStart(t *testing.T) {
	cmds := []*exec.Cmd{
		exec.Command("sh", "-c", "echo hello 1>&2"),
		exec.Command("sh", "-c", "echo world"),
		exec.Command("nosuchcommand"),
		exec.Command("sh", "-c", "exit 1"),
	}
	lines, started, exited := cmdplx.Start(cmds)
	closed := 0
	var (
		exitError       *cmdplx.Status
		commandNotFound *cmdplx.Status
	)

	for {
		select {
		case line := <-lines:
			if line == nil {
				lines = nil
				if closed++; closed == 3 {
					goto DONE
				}
				break
			}
			if line.From() == 1 {
				if text := line.Text(); text != "world" {
					t.Errorf("expect 'world', got %s", text)
				}
			}
			if line.From() == 2 {
				if text := line.Text(); text != "hello" {
					t.Errorf("expect 'hello', got %s", text)
				}
			}
		case status := <-started:
			if status == nil {
				started = nil
				if closed++; closed == 3 {
					goto DONE
				}
				break
			}
			if err := status.Err(); err != nil {
				commandNotFound = status
			}
		case status := <-exited:
			if status == nil {
				exited = nil
				if closed++; closed == 3 {
					goto DONE
				}
				break
			}
			if err := status.Err(); err != nil {
				exitError = status
			}
		}
	}
DONE:

	if exitError == nil || exitError.Cmd() != cmds[3] {
		t.Errorf("wrong exit status")
	}

	if commandNotFound == nil || commandNotFound.Cmd() != cmds[2] {
		t.Errorf("wrong command not found status")
	}
}