From 836763c002a63ad88ad7dbbef661ceeddde3e14b Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 5 Jan 2021 12:24:25 +0100 Subject: [PATCH] refactor: run commands natively --- src/environment.go | 59 +++------------------------------------------- 1 file changed, 3 insertions(+), 56 deletions(-) diff --git a/src/environment.go b/src/environment.go index 11f66081..9065a43a 100644 --- a/src/environment.go +++ b/src/environment.go @@ -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 {