From 5d038dfd334d4bdd767ac4db504a21160a013f79 Mon Sep 17 00:00:00 2001 From: mjarkk Date: Wed, 12 Dec 2018 22:10:33 +0100 Subject: [PATCH] Removed the wired error handling --- pkg/commands/exec_live_default.go | 19 +++++++++++-------- pkg/commands/exec_live_win.go | 5 ++--- pkg/commands/os.go | 12 +++--------- pkg/gui/view_helpers.go | 2 +- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/pkg/commands/exec_live_default.go b/pkg/commands/exec_live_default.go index 9f08b014b..acc0bcf18 100644 --- a/pkg/commands/exec_live_default.go +++ b/pkg/commands/exec_live_default.go @@ -4,6 +4,7 @@ package commands import ( "bufio" + "errors" "os" "os/exec" "strings" @@ -19,8 +20,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) (errorMessage string, codeError error) { +func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) error { cmdOutput := []string{} + cmdOutputOffset := 0 splitCmd := str.ToArgv(command) cmd := exec.Command(splitCmd[0], splitCmd[1:]...) @@ -30,12 +32,8 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s tty, err := pty.Start(cmd) - // go func() { - // _ = tty.Close() - // }() - if err != nil { - return "", err + return err } var waitForBufio sync.WaitGroup @@ -49,6 +47,8 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s cmdOutput = append(cmdOutput, toOutput) toWrite := output(toOutput) if len(toWrite) > 0 { + // don't do -1 because the next value is the username / password + cmdOutputOffset = len(cmdOutput) _, _ = tty.Write([]byte(toWrite + "\n")) } } @@ -59,10 +59,13 @@ func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(s tty.Close() if err != nil { waitForBufio.Wait() - return strings.Join(cmdOutput, " "), err + if len(cmdOutput) == cmdOutputOffset { + cmdOutputOffset-- + } + return errors.New(err.Error() + ", " + strings.Join(cmdOutput[cmdOutputOffset:], " ")) } - return errorMessage, nil + return nil } // scanWordsWithNewLines is a copy of bufio.ScanWords but this also captures new lines diff --git a/pkg/commands/exec_live_win.go b/pkg/commands/exec_live_win.go index d97274279..343e0dd9f 100644 --- a/pkg/commands/exec_live_win.go +++ b/pkg/commands/exec_live_win.go @@ -4,7 +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) (errorMessage string, codeError error) { - cmdOputput := c.RunCommand(command) - return cmdOputput.Error(), cmdOputput +func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) error { + return c.RunCommand(command) } diff --git a/pkg/commands/os.go b/pkg/commands/os.go index d40a60e46..827e6c420 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -59,7 +59,7 @@ func (c *OSCommand) RunCommandWithOutput(command string) (string, error) { } // RunCommandWithOutputLive runs RunCommandWithOutputLiveWrapper -func (c *OSCommand) RunCommandWithOutputLive(command string, output func(string) string) (errorMessage string, err error) { +func (c *OSCommand) RunCommandWithOutputLive(command string, output func(string) string) error { return RunCommandWithOutputLiveWrapper(c, command, output) } @@ -68,7 +68,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 := "" - errMessage, err := c.RunCommandWithOutputLive(command, func(word string) string { + errMessage := c.RunCommandWithOutputLive(command, func(word string) string { ttyText = ttyText + " " + word prompts := map[string]string{ @@ -85,13 +85,7 @@ func (c *OSCommand) DetectUnamePass(command string, ask func(string) string) err return "" }) - if err != nil { - if strings.Contains("exit status 128", err.Error()) { - errMessage = "exit status 128" - } - return errors.New(errMessage) - } - return nil + return errMessage } // RunCommand runs a command and just returns the error diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index fa676a47d..883773926 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -320,7 +320,7 @@ func (gui *Gui) HandleCredentialsPopup(g *gocui.Gui, popupOpened bool, cmdErr er } if cmdErr != nil { errMessage := cmdErr.Error() - if errMessage == "exit status 128" { + if strings.Contains(errMessage, "exit status 128") { errMessage = gui.Tr.SLocalize("PassUnameWrong") } _ = gui.createErrorPanel(g, errMessage)