1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-19 00:28:03 +02:00

standardise error handling of command functions

This commit is contained in:
Jesse Duffield
2018-08-06 19:01:27 +10:00
parent b1918f2f68
commit cf73d4f558

View File

@ -119,8 +119,7 @@ func runDirectCommand(command string) (string, error) {
Command("bash", "-c", command). Command("bash", "-c", command).
CombinedOutput() CombinedOutput()
devLog("run direct command time for command: ", command, time.Now().Sub(timeStart)) devLog("run direct command time for command: ", command, time.Now().Sub(timeStart))
return sanitisedCommandOutput(cmdOut, err)
return string(cmdOut), err
} }
func branchStringParts(branchString string) (string, string) { func branchStringParts(branchString string) (string, string) {
@ -299,17 +298,21 @@ func gitCheckout(branch string, force bool) (string, error) {
return runCommand("git checkout " + forceArg + branch) 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) { func runCommand(command string) (string, error) {
commandStartTime := time.Now() commandStartTime := time.Now()
commandLog(command) commandLog(command)
splitCmd := strings.Split(command, " ") splitCmd := strings.Split(command, " ")
cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput() cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput()
devLog("run command time: ", time.Now().Sub(commandStartTime)) devLog("run command time: ", time.Now().Sub(commandStartTime))
outputString := string(cmdOut) return sanitisedCommandOutput(cmdOut, err)
if outputString == "" && err != nil {
return err.Error(), err
}
return outputString, err
} }
func openFile(filename string) (string, error) { func openFile(filename string) (string, error) {
@ -446,7 +449,7 @@ func removeFile(file GitFile) error {
} }
func gitCommit(message string) (string, error) { func gitCommit(message string) (string, error) {
return runCommand("git commit -m \"" + message + "\"") return runDirectCommand("git commit -m \"" + message + "\"")
} }
func gitPull() (string, error) { func gitPull() (string, error) {