refactor: run commands natively

This commit is contained in:
Jan De Dobbeleer 2021-01-05 12:24:25 +01:00 committed by Jan De Dobbeleer
parent 598984b4cb
commit 836763c002

View file

@ -1,10 +1,7 @@
package main
import (
"bufio"
"bytes"
"context"
"io"
"io/ioutil"
"log"
"net/http"
@ -155,61 +152,11 @@ func (env *environment) getPlatform() string {
}
func (env *environment) runCommand(command string, args ...string) (string, error) {
getOutputString := func(io io.ReadCloser) string {
output := new(bytes.Buffer)
defer output.Reset()
buf := bufio.NewReader(io)
multiline := false
for {
line, _, _ := buf.ReadLine()
if line == nil {
break
}
if multiline {
output.WriteString("\n")
}
output.Write(line)
multiline = true
}
return output.String()
}
cmd := exec.Command(command, args...)
stdout, err := cmd.StdoutPipe()
out, err := exec.Command(command, args...).Output()
if err != nil {
return "", &commandError{
err: err.Error(),
exitCode: 666,
return "", err
}
}
stderr, err := cmd.StderrPipe()
if err != nil {
return "", &commandError{
err: err.Error(),
exitCode: 667,
}
}
err = cmd.Start()
if err != nil {
return "", &commandError{
err: err.Error(),
exitCode: 668,
}
}
defer func() {
_ = cmd.Process.Kill()
}()
stdoutString := getOutputString(stdout)
stderrString := getOutputString(stderr)
if stderrString != "" {
// only wait in case of error reduces the lead time on successful
// commands on windows due to not calling process.Wait()
_ = cmd.Wait()
return "", &commandError{
err: stderrString,
exitCode: cmd.ProcessState.ExitCode(),
}
}
return stdoutString, nil
return strings.TrimSpace(string(out)), nil
}
func (env *environment) runShellCommand(shell, command string) string {