Rework Launch() into GetScanner() so command can be Start()'ed ad-hoc
This commit is contained in:
parent
3626d5ff1b
commit
203dbb8592
3 changed files with 15 additions and 16 deletions
|
@ -2,7 +2,6 @@ package scan
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"log"
|
||||
"os/exec"
|
||||
)
|
||||
|
@ -13,23 +12,20 @@ type Command struct {
|
|||
cmd *exec.Cmd
|
||||
}
|
||||
|
||||
func (command *Command) Launch() *bufio.Scanner {
|
||||
func (command *Command) GetScanner() *bufio.Scanner {
|
||||
command.cmd = exec.Command(command.Name, command.Args...)
|
||||
|
||||
stderr, err := command.cmd.StderrPipe()
|
||||
if err != nil {
|
||||
log.Fatalf("could not get stderr pipe: %v", err)
|
||||
}
|
||||
|
||||
stdout, err := command.cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.Fatalf("could not get stdout pipe: %v", err)
|
||||
}
|
||||
command.cmd.Stderr = command.cmd.Stdout
|
||||
|
||||
merged := io.MultiReader(stdout, stderr)
|
||||
// command.cmd.Start()
|
||||
return bufio.NewScanner(stdout)
|
||||
}
|
||||
|
||||
return bufio.NewScanner(merged)
|
||||
func (command *Command) Start() {
|
||||
command.cmd.Start()
|
||||
}
|
||||
|
||||
func (command *Command) Wait() {
|
||||
|
|
|
@ -8,18 +8,21 @@ import (
|
|||
func TestCommandRun(t *testing.T) {
|
||||
command := Command{Name: "echo", Args: []string{"Here", "be", "a", "test"}}
|
||||
|
||||
scanner := command.Launch()
|
||||
scanner := command.GetScanner()
|
||||
command.Start()
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
if text != "Here be a test" {
|
||||
t.Error("Didn't do shit with" + text)
|
||||
}
|
||||
}
|
||||
command.Wait()
|
||||
}
|
||||
func TestCommandOutput(t *testing.T) {
|
||||
command := Command{Name: "go", Args: []string{"version"}}
|
||||
|
||||
scanner := command.Launch()
|
||||
scanner := command.GetScanner()
|
||||
command.Start()
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
if !strings.HasPrefix(text, "go version") {
|
||||
|
@ -37,7 +40,8 @@ func TestCommandOutput(t *testing.T) {
|
|||
|
||||
func TestCommandFailure(t *testing.T) {
|
||||
command := Command{Name: "go", Args: []string{"gahdned"}}
|
||||
scanner := command.Launch()
|
||||
scanner := command.GetScanner()
|
||||
command.Start()
|
||||
var text []string
|
||||
for scanner.Scan() {
|
||||
text = append(text, scanner.Text())
|
||||
|
|
|
@ -30,8 +30,9 @@ func Process(basedir string, document *Document) string {
|
|||
|
||||
for _, command := range commands {
|
||||
done := make(chan bool)
|
||||
scanner := command.Launch()
|
||||
scanner := command.GetScanner()
|
||||
|
||||
command.Start()
|
||||
go func() {
|
||||
document.Events <- command.Name + " Startet"
|
||||
|
||||
|
@ -43,8 +44,6 @@ func Process(basedir string, document *Document) string {
|
|||
done <- true
|
||||
document.Events <- command.Name + " Ist gestartet"
|
||||
}()
|
||||
|
||||
command.cmd.Start()
|
||||
command.Wait()
|
||||
<-done
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue