Skip to content

Commit

Permalink
feat: faster command time
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Nov 19, 2020
1 parent 0250e8d commit 408b94f
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions environment.go
@@ -1,7 +1,8 @@
package main

import (
"errors"
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -138,17 +139,34 @@ func (env *environment) getPlatform() string {
}

func (env *environment) runCommand(command string, args ...string) (string, error) {
out, err := exec.Command(command, args...).Output()

var exerr *exec.ExitError
if errors.As(err, &exerr) {
return "", &commandError{exitCode: exerr.ExitCode()}
cmd := exec.Command(command, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
err = cmd.Start()
if err != nil {
return "", err
}

return strings.TrimSpace(string(out)), nil
defer func() {
_ = 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 {
Expand Down

0 comments on commit 408b94f

Please sign in to comment.