From c9944dc7a7ca42d08b327d8376a540b47c71c43c Mon Sep 17 00:00:00 2001 From: lnu Date: Wed, 30 Dec 2020 13:11:40 +0100 Subject: [PATCH] fix: restore old runCommand code + dotnet exit code check respect code 145 for dotnet segment --- src/environment.go | 46 +++++++---------------------------------- src/segment_language.go | 2 +- 2 files changed, 8 insertions(+), 40 deletions(-) diff --git a/src/environment.go b/src/environment.go index 1d2ff9392e7d..e70b09d08d36 100644 --- a/src/environment.go +++ b/src/environment.go @@ -1,12 +1,9 @@ package main import ( - "bufio" - "bytes" "context" "errors" "fmt" - "io" "io/ioutil" "log" "net/http" @@ -155,46 +152,17 @@ 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() - if err != nil { - log.Fatal(err) - } - stderr, err := cmd.StderrPipe() - if err != nil { - log.Fatal(err) + out, err := exec.Command(command, args...).Output() + + var exerr *exec.ExitError + if errors.As(err, &exerr) { + return "", &commandError{exitCode: exerr.ExitCode()} } - err = cmd.Start() if err != nil { return "", err } - defer func() { - _ = cmd.Process.Kill() - }() - stdoutString := getOutputString(stdout) - stderrString := getOutputString(stderr) - if stderrString != "" { - return "", errors.New(stderrString) - } - return stdoutString, nil + + return strings.TrimSpace(string(out)), nil } func (env *environment) runShellCommand(shell, command string) string { diff --git a/src/segment_language.go b/src/segment_language.go index 60edd8690964..ca43b223bc6a 100644 --- a/src/segment_language.go +++ b/src/segment_language.go @@ -36,7 +36,7 @@ func (l *language) string() string { } // call getVersion if displayVersion set in config - if l.props.getBool(DisplayVersion, true) && l.hasCommand() && l.getVersion() { + if l.props.getBool(DisplayVersion, true) && l.getVersion() { return l.version } return ""