Skip to content

Commit

Permalink
fix: restore old runCommand code + dotnet exit code check
Browse files Browse the repository at this point in the history
respect code 145 for dotnet segment
  • Loading branch information
lnu committed Dec 30, 2020
1 parent 4e96b4c commit c9944dc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 40 deletions.
46 changes: 7 additions & 39 deletions src/environment.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package main

import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/segment_language.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down

0 comments on commit c9944dc

Please sign in to comment.