summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormjarkk <mkopenga@gmail.com>2018-10-29 08:23:56 +0100
committermjarkk <mkopenga@gmail.com>2018-10-29 08:23:56 +0100
commit9585f494906df62f26202b5c92d5cff027a10a36 (patch)
treed1d900a095cca9008fb4fe3a208a40f3658aa9d3
parent1e0310a86d61356d68c77a8e64bb572f0aaabeda (diff)
Made error handling better
-rw-r--r--pkg/commands/exec_live_default.go16
-rw-r--r--pkg/commands/exec_live_win.go4
-rw-r--r--pkg/commands/git_test.go13
-rw-r--r--pkg/commands/os.go10
4 files changed, 26 insertions, 17 deletions
diff --git a/pkg/commands/exec_live_default.go b/pkg/commands/exec_live_default.go
index 6c0ac3077..f8755de26 100644
--- a/pkg/commands/exec_live_default.go
+++ b/pkg/commands/exec_live_default.go
@@ -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
}
diff --git a/pkg/commands/exec_live_win.go b/pkg/commands/exec_live_win.go
index 343e0dd9f..835d7da68 100644
--- a/pkg/commands/exec_live_win.go
+++ b/pkg/commands/exec_live_win.go
@@ -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)
}
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go
index c99479a58..bef067624 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -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)
})
}
}
diff --git a/pkg/commands/os.go b/pkg/commands/os.go
index 28c01813b..dca0c8eae 100644
--- a/pkg/commands/os.go
+++ b/pkg/commands/os.go
@@ -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
}