Skip to content

Commit 836763c

Browse files
refactor: run commands natively
1 parent 598984b commit 836763c

File tree

1 file changed

+3
-56
lines changed

1 file changed

+3
-56
lines changed

src/environment.go

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package main
22

33
import (
4-
"bufio"
5-
"bytes"
64
"context"
7-
"io"
85
"io/ioutil"
96
"log"
107
"net/http"
@@ -155,61 +152,11 @@ func (env *environment) getPlatform() string {
155152
}
156153

157154
func (env *environment) runCommand(command string, args ...string) (string, error) {
158-
getOutputString := func(io io.ReadCloser) string {
159-
output := new(bytes.Buffer)
160-
defer output.Reset()
161-
buf := bufio.NewReader(io)
162-
multiline := false
163-
for {
164-
line, _, _ := buf.ReadLine()
165-
if line == nil {
166-
break
167-
}
168-
if multiline {
169-
output.WriteString("\n")
170-
}
171-
output.Write(line)
172-
multiline = true
173-
}
174-
return output.String()
175-
}
176-
cmd := exec.Command(command, args...)
177-
stdout, err := cmd.StdoutPipe()
178-
if err != nil {
179-
return "", &commandError{
180-
err: err.Error(),
181-
exitCode: 666,
182-
}
183-
}
184-
stderr, err := cmd.StderrPipe()
185-
if err != nil {
186-
return "", &commandError{
187-
err: err.Error(),
188-
exitCode: 667,
189-
}
190-
}
191-
err = cmd.Start()
155+
out, err := exec.Command(command, args...).Output()
192156
if err != nil {
193-
return "", &commandError{
194-
err: err.Error(),
195-
exitCode: 668,
196-
}
197-
}
198-
defer func() {
199-
_ = cmd.Process.Kill()
200-
}()
201-
stdoutString := getOutputString(stdout)
202-
stderrString := getOutputString(stderr)
203-
if stderrString != "" {
204-
// only wait in case of error reduces the lead time on successful
205-
// commands on windows due to not calling process.Wait()
206-
_ = cmd.Wait()
207-
return "", &commandError{
208-
err: stderrString,
209-
exitCode: cmd.ProcessState.ExitCode(),
210-
}
157+
return "", err
211158
}
212-
return stdoutString, nil
159+
return strings.TrimSpace(string(out)), nil
213160
}
214161

215162
func (env *environment) runShellCommand(shell, command string) string {

0 commit comments

Comments
 (0)