Skip to content

Commit

Permalink
fix: error output now properly included in return
Browse files Browse the repository at this point in the history
  • Loading branch information
furan917 committed Oct 11, 2023
1 parent ab73a40 commit 7da6a34
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions magerun/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func HandleMagerunCommand(messageBody string) (string, error) {

func executeMagerunCommand(args []string) (string, error) {
mageRunCmdPath := getMageRunCommand()
logger.Infof("Executing command %s with args: %v\n", mageRunCmdPath, args)
logger.Infof("Executing command %s with args: %v", mageRunCmdPath, args)

if config_manager.GetBoolValue(config_manager.ConfigSlackEnabled) {
logger.Infof("Slack notification is enabled, sending notification")
Expand All @@ -71,16 +71,16 @@ func executeMagerunCommand(args []string) (string, error) {
cmd.Stderr = &stderrBuffer

err := cmd.Run()
if err != nil {
return "", fmt.Errorf("error executing magerun command: %s", err)
}

// Grab any output before returning with command error
stdoutStr := stdoutBuffer.String()
stderrStr := stderrBuffer.String()

output := stripMagerunOutput(stdoutStr + "\n" + stderrStr)

logger.Infof("Executed command %s with args: %v and handling output", mageRunCmdPath, args)
// Now check command for error and return either success or failure
if err != nil {
logger.Warnf("Error executing magerun command: %s, with the following output: %s", err, strings.ReplaceAll(output, "\n", " "))
return output, fmt.Errorf("error executing magerun command: %s", err)
}
return output, nil
}

Expand All @@ -95,6 +95,8 @@ func stripMagerunOutput(output string) string {
re := regexp.MustCompile(pattern)
strippedOutput = re.ReplaceAllString(strippedOutput, replacement)
}
//trim any leading or trailing whitespace
strippedOutput = strings.TrimSpace(strippedOutput)

return strippedOutput
}
Expand Down

0 comments on commit 7da6a34

Please sign in to comment.