Skip to content

Commit

Permalink
fix: use commandError on cmd failure
Browse files Browse the repository at this point in the history
relates to #285
  • Loading branch information
JanDeDobbeleer committed Dec 31, 2020
1 parent e0a4482 commit 0e8d6c7
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -58,11 +56,12 @@ type environment struct {
}

type commandError struct {
err string
exitCode int
}

func (e *commandError) Error() string {
return fmt.Sprintf("%d", e.exitCode)
return e.err
}

func (env *environment) getenv(key string) string {
Expand Down Expand Up @@ -176,23 +175,35 @@ func (env *environment) runCommand(command string, args ...string) (string, erro
cmd := exec.Command(command, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
return "", &commandError{
err: err.Error(),
exitCode: 666,
}
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
return "", &commandError{
err: err.Error(),
exitCode: 667,
}
}
err = cmd.Start()
if err != nil {
return "", err
return "", &commandError{
err: err.Error(),
exitCode: 668,
}
}
defer func() {
_ = cmd.Process.Kill()
}()
stdoutString := getOutputString(stdout)
stderrString := getOutputString(stderr)
if stderrString != "" {
return "", errors.New(stderrString)
return "", &commandError{
err: stderrString,
exitCode: cmd.ProcessState.ExitCode(),
}
}
return stdoutString, nil
}
Expand Down

0 comments on commit 0e8d6c7

Please sign in to comment.