1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-20 05:19:24 +02:00

Made error handling better

This commit is contained in:
mjarkk 2018-10-29 08:23:56 +01:00
parent 1e0310a86d
commit 9585f49490
4 changed files with 26 additions and 17 deletions

View File

@ -4,10 +4,10 @@ package commands
import ( import (
"bufio" "bufio"
"errors"
"os" "os"
"os/exec" "os/exec"
"regexp" "regexp"
"strings"
"github.com/kr/pty" "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 // 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: 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 // 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) splitCmd := ToArgv(command)
cmd := exec.Command(splitCmd[0], splitCmd[1:]...) 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) tty, err := pty.Start(cmd)
if err != nil { if err != nil {
return errors.New(err.Error()) return errorMessage, err
} }
defer func() { _ = tty.Close() }() defer func() { _ = tty.Close() }()
@ -40,7 +42,9 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s
scanner := bufio.NewScanner(tty) scanner := bufio.NewScanner(tty)
scanner.Split(bufio.ScanWords) scanner.Split(bufio.ScanWords)
for scanner.Scan() { 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 { if len(toWrite) > 0 {
_, _ = tty.Write([]byte(toWrite + "\n")) _, _ = tty.Write([]byte(toWrite + "\n"))
} }
@ -48,8 +52,8 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s
}() }()
if err := cmd.Wait(); err != nil { if err := cmd.Wait(); err != nil {
return errors.New(err.Error()) return strings.Join(cmdOutput, " "), err
} }
return nil return errorMessage, nil
} }

View File

@ -4,6 +4,6 @@ package commands
// RunCommandWithOutputLiveWrapper runs a command live but because of windows compatibility this command can't be ran there // 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 // 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 { func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) (errorMessage string, codeError error) {
return c.RunCommand(command) return errorMessage, c.RunCommand(command)
} }

View File

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"strings"
"testing" "testing"
"time" "time"
@ -1008,23 +1009,23 @@ func TestGitCommandPush(t *testing.T) {
}, },
false, false,
func(err error) { 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) { t.Run(s.testName, func(t *testing.T) {
gitCmd := newDummyGitCommand() gitCmd := newDummyGitCommand()
gitCmd.OSCommand.command = s.command gitCmd.OSCommand.command = s.command
err := gitCmd.Push("test", s.forcePush, func(passOrUname string) string { err := gitCmd.Push("test", s.forcePush, func(passOrUname string) string {
return "-" return "-"
}) })
if err.Error() == "exit status 1" && i != 2 { errMessage := err.Error()
s.test(nil) if strings.Contains(errMessage, "error: src refspec test does not match any.") {
} else { err = nil
s.test(err)
} }
s.test(err)
}) })
} }
} }

View File

@ -57,7 +57,7 @@ func (c *OSCommand) RunCommandWithOutput(command string) (string, error) {
} }
// RunCommandWithOutputLive runs RunCommandWithOutputLiveWrapper // 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) 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 // 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 { func (c *OSCommand) DetectUnamePass(command string, ask func(string) string) error {
ttyText := "" ttyText := ""
err := c.RunCommandWithOutputLive(command, func(word string) string { errMessage, err := c.RunCommandWithOutputLive(command, func(word string) string {
ttyText = ttyText + " " + word ttyText = ttyText + " " + word
// detect username question // detect username question
@ -87,7 +87,11 @@ func (c *OSCommand) DetectUnamePass(command string, ask func(string) string) err
return "" return ""
}) })
if err != nil { if err != nil {
return err errorCode := err.Error()
if errorCode == "exit status 128" {
errMessage = errorCode
}
return errors.New(errMessage)
} }
return nil return nil
} }