feat: faster command time

This commit is contained in:
Jan De Dobbeleer 2020-11-19 09:02:51 +01:00 committed by Jan De Dobbeleer
parent 0250e8d5d1
commit 408b94fcd4

View file

@ -1,7 +1,8 @@
package main package main
import ( import (
"errors" "bufio"
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -138,17 +139,34 @@ func (env *environment) getPlatform() string {
} }
func (env *environment) runCommand(command string, args ...string) (string, error) { func (env *environment) runCommand(command string, args ...string) (string, error) {
out, err := exec.Command(command, args...).Output() cmd := exec.Command(command, args...)
stdout, err := cmd.StdoutPipe()
var exerr *exec.ExitError if err != nil {
if errors.As(err, &exerr) { log.Fatal(err)
return "", &commandError{exitCode: exerr.ExitCode()}
} }
err = cmd.Start()
if err != nil { if err != nil {
return "", err return "", err
} }
defer func() {
return strings.TrimSpace(string(out)), nil _ = cmd.Process.Kill()
}()
output := new(bytes.Buffer)
defer output.Reset()
buf := bufio.NewReader(stdout)
multiline := false
for {
line, _, _ := buf.ReadLine()
if line == nil {
break
}
if multiline {
output.WriteString("\n")
}
output.Write(line)
multiline = true
}
return output.String(), nil
} }
func (env *environment) runShellCommand(shell, command string) string { func (env *environment) runShellCommand(shell, command string) string {