Skip to content

Commit

Permalink
fix issue where exec did not receive all output
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Nov 24, 2015
1 parent ab54ab1 commit 4b3cc3e
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions command.go
@@ -1,39 +1,26 @@
package main

import (
"bufio"
"bytes"
"fmt"
"os/exec"
"syscall"
)

// A Command contains the binary executable to be run when executing commands.
type Command struct {
Bin string
bin string
}

// Exec runs the specified command and returns its output and exit code.
func (c *Command) Exec(cmds ...string) (string, int) {
cmd := exec.Command(c.Bin, cmds...)
cmd := exec.Command(c.bin, cmds...)

stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdout = stdout
cmd.Stderr = stderr

reader, _ := cmd.StdoutPipe()
scanner := bufio.NewScanner(reader)
go func() {
for scanner.Scan() {
stdout.WriteString(fmt.Sprintf("%s\n", scanner.Bytes()))
}
}()

if err := cmd.Start(); err != nil {
return "Failed to spawn command\n", 1
}

if err := cmd.Wait(); err != nil {
if err := cmd.Run(); err != nil {
code := 1

// Make sure we catch errors and return the correct exit code, if possible
Expand Down

0 comments on commit 4b3cc3e

Please sign in to comment.