mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-04 03:48:07 +02:00
Made error handling better
This commit is contained in:
parent
1e0310a86d
commit
9585f49490
@ -4,10 +4,10 @@ package commands
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/kr/pty"
|
||||
)
|
||||
@ -17,7 +17,9 @@ import (
|
||||
// As return of output you need to give a string that will be written to stdin
|
||||
// NOTE: If the return data is empty it won't written anything to stdin
|
||||
// NOTE: You don't have to include a enter in the return data this function will do that for you
|
||||
func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) error {
|
||||
func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) (errorMessage string, codeError error) {
|
||||
cmdOutput := []string{}
|
||||
|
||||
splitCmd := ToArgv(command)
|
||||
cmd := exec.Command(splitCmd[0], splitCmd[1:]...)
|
||||
|
||||
@ -27,7 +29,7 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s
|
||||
tty, err := pty.Start(cmd)
|
||||
|
||||
if err != nil {
|
||||
return errors.New(err.Error())
|
||||
return errorMessage, err
|
||||
}
|
||||
|
||||
defer func() { _ = tty.Close() }()
|
||||
@ -40,7 +42,9 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s
|
||||
scanner := bufio.NewScanner(tty)
|
||||
scanner.Split(bufio.ScanWords)
|
||||
for scanner.Scan() {
|
||||
toWrite := output(re.ReplaceAllString(scanner.Text(), ""))
|
||||
toOutput := re.ReplaceAllString(scanner.Text(), "")
|
||||
cmdOutput = append(cmdOutput, toOutput)
|
||||
toWrite := output(toOutput)
|
||||
if len(toWrite) > 0 {
|
||||
_, _ = tty.Write([]byte(toWrite + "\n"))
|
||||
}
|
||||
@ -48,8 +52,8 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s
|
||||
}()
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
return errors.New(err.Error())
|
||||
return strings.Join(cmdOutput, " "), err
|
||||
}
|
||||
|
||||
return nil
|
||||
return errorMessage, nil
|
||||
}
|
||||
|
@ -4,6 +4,6 @@ package commands
|
||||
|
||||
// RunCommandWithOutputLiveWrapper runs a command live but because of windows compatibility this command can't be ran there
|
||||
// TODO: Remove this hack and replace it with a propper way to run commands live on windows
|
||||
func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) error {
|
||||
return c.RunCommand(command)
|
||||
func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) (errorMessage string, codeError error) {
|
||||
return errorMessage, c.RunCommand(command)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -1008,23 +1009,23 @@ func TestGitCommandPush(t *testing.T) {
|
||||
},
|
||||
false,
|
||||
func(err error) {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, err)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
for _, s := range scenarios {
|
||||
t.Run(s.testName, func(t *testing.T) {
|
||||
gitCmd := newDummyGitCommand()
|
||||
gitCmd.OSCommand.command = s.command
|
||||
err := gitCmd.Push("test", s.forcePush, func(passOrUname string) string {
|
||||
return "-"
|
||||
})
|
||||
if err.Error() == "exit status 1" && i != 2 {
|
||||
s.test(nil)
|
||||
} else {
|
||||
s.test(err)
|
||||
errMessage := err.Error()
|
||||
if strings.Contains(errMessage, "error: src refspec test does not match any.") {
|
||||
err = nil
|
||||
}
|
||||
s.test(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ func (c *OSCommand) RunCommandWithOutput(command string) (string, error) {
|
||||
}
|
||||
|
||||
// RunCommandWithOutputLive runs RunCommandWithOutputLiveWrapper
|
||||
func (c *OSCommand) RunCommandWithOutputLive(command string, output func(string) string) error {
|
||||
func (c *OSCommand) RunCommandWithOutputLive(command string, output func(string) string) (errorMessage string, err error) {
|
||||
return RunCommandWithOutputLiveWrapper(c, command, output)
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ func (c *OSCommand) RunCommandWithOutputLive(command string, output func(string)
|
||||
// The ask argument will be "username" or "password" and expects the user's password or username back
|
||||
func (c *OSCommand) DetectUnamePass(command string, ask func(string) string) error {
|
||||
ttyText := ""
|
||||
err := c.RunCommandWithOutputLive(command, func(word string) string {
|
||||
errMessage, err := c.RunCommandWithOutputLive(command, func(word string) string {
|
||||
ttyText = ttyText + " " + word
|
||||
|
||||
// detect username question
|
||||
@ -87,7 +87,11 @@ func (c *OSCommand) DetectUnamePass(command string, ask func(string) string) err
|
||||
return ""
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
errorCode := err.Error()
|
||||
if errorCode == "exit status 128" {
|
||||
errMessage = errorCode
|
||||
}
|
||||
return errors.New(errMessage)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user