From cf73d4f558a1c180a52f9be4768aa9b754d4b4b3 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 6 Aug 2018 19:01:27 +1000 Subject: [PATCH] standardise error handling of command functions --- gitcommands.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/gitcommands.go b/gitcommands.go index d537672a0..27e4e8369 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -119,8 +119,7 @@ func runDirectCommand(command string) (string, error) { Command("bash", "-c", command). CombinedOutput() devLog("run direct command time for command: ", command, time.Now().Sub(timeStart)) - - return string(cmdOut), err + return sanitisedCommandOutput(cmdOut, err) } func branchStringParts(branchString string) (string, string) { @@ -299,17 +298,21 @@ func gitCheckout(branch string, force bool) (string, error) { return runCommand("git checkout " + forceArg + branch) } +func sanitisedCommandOutput(output []byte, err error) (string, error) { + outputString := string(output) + if outputString == "" && err != nil { + return err.Error(), err + } + return outputString, err +} + func runCommand(command string) (string, error) { commandStartTime := time.Now() commandLog(command) splitCmd := strings.Split(command, " ") cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput() devLog("run command time: ", time.Now().Sub(commandStartTime)) - outputString := string(cmdOut) - if outputString == "" && err != nil { - return err.Error(), err - } - return outputString, err + return sanitisedCommandOutput(cmdOut, err) } func openFile(filename string) (string, error) { @@ -446,7 +449,7 @@ func removeFile(file GitFile) error { } func gitCommit(message string) (string, error) { - return runCommand("git commit -m \"" + message + "\"") + return runDirectCommand("git commit -m \"" + message + "\"") } func gitPull() (string, error) {