mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-31 22:07:25 -08:00
refactor: run commands natively
This commit is contained in:
parent
598984b4cb
commit
836763c002
|
@ -1,10 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -155,61 +152,11 @@ func (env *environment) getPlatform() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *environment) runCommand(command string, args ...string) (string, error) {
|
func (env *environment) runCommand(command string, args ...string) (string, error) {
|
||||||
getOutputString := func(io io.ReadCloser) string {
|
out, err := exec.Command(command, args...).Output()
|
||||||
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 {
|
if err != nil {
|
||||||
return "", &commandError{
|
return "", err
|
||||||
err: err.Error(),
|
|
||||||
exitCode: 666,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
stderr, err := cmd.StderrPipe()
|
return strings.TrimSpace(string(out)), nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *environment) runShellCommand(shell, command string) string {
|
func (env *environment) runShellCommand(shell, command string) string {
|
||||||
|
|
Loading…
Reference in a new issue