Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
Feature: 'error_exit_code' flag;
Browse files Browse the repository at this point in the history
  • Loading branch information
KonH committed Apr 20, 2018
1 parent 844ffb9 commit 6b92051
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ You can use several servers (as many as you want), but if you don't need to dupl

- Command output is shown as "message"
- If "logfile" exists, "message" does not contain actual message, but contains path to log
- To catch errors using non-zero exit code, use "error_exit_code"
- To catch errors in output, use "error_regex"
- To convert message to short "result", use "result_regex" (also, you can set "result_right_to_left" to "true" if you need to catch last match instead of first)
- If application which you call can't write to stdout and support only external log files, you can enable "is_external_log" (optional) and provide log path to app and "log_file". When execution is done, log file will be processed as usual
Expand All @@ -250,6 +251,7 @@ You can use several servers (as many as you want), but if you don't need to dupl
"args": "arguments",
"work_dir": "work_directory",
"error_regex": "regex_to_catch_errors",
"error_exit_code": "true",
"log_file": "path_to_logfile",
"is_external_log": "false"
}
Expand Down
35 changes: 25 additions & 10 deletions Server/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ public class RunCommand:ICommand, IAbortableCommand {
if (_isAborted) {
throw new CommandAbortedException();
}
var errorRegex = args.Get("error_regex");
var resultRegex = args.Get("result_regex");
var checkResultValue = !string.IsNullOrEmpty(resultRegex);
var isRightToLeft = args.Get("result_right_to_left");
var errorRegex = args.Get("error_regex");
var errorExitCode = args.GetBoolean("error_exit_code", false);
var resultRegex = args.Get("result_regex");
var checkResultValue = !string.IsNullOrEmpty(resultRegex);
var isRightToLeft = args.Get("result_right_to_left");
var isRightToLeftValue = !string.IsNullOrEmpty(isRightToLeft) && bool.Parse(isRightToLeft);
var messageToCheck = string.Empty;
var messageToShow = string.Empty;
if (!string.IsNullOrEmpty(logFile)) {
var msg = $"Log saved to {logFile}.";
string logContent = null;
Expand All @@ -88,14 +91,18 @@ public class RunCommand:ICommand, IAbortableCommand {
}
}
} while (!isDone);
var result = GetResultMessage(resultRegex, logContent, isRightToLeftValue);
return CheckCommandResult(errorRegex, logContent, msg, checkResultValue, result);
messageToCheck = logContent;
messageToShow = msg;
} else {
_inMemoryLog = _inMemoryLog.TrimEnd('\n');
var msg = _inMemoryLog;
var result = GetResultMessage(resultRegex, msg, isRightToLeftValue);
return CheckCommandResult(errorRegex, msg, msg, checkResultValue, result);
_inMemoryLog = _inMemoryLog.TrimEnd('\n');
messageToCheck = _inMemoryLog;
messageToShow = _inMemoryLog;
}
var result = GetResultMessage(resultRegex, messageToCheck, isRightToLeftValue);
if ( errorExitCode ) {
return CheckCommandResultViaExitCode(process, messageToShow, result);
}
return CheckCommandResult(errorRegex, messageToCheck, messageToShow, checkResultValue, result);
}
catch (Exception e) {
if (e is CommandAbortedException) {
Expand Down Expand Up @@ -160,6 +167,14 @@ public class RunCommand:ICommand, IAbortableCommand {
return false;
}

CommandResult CheckCommandResultViaExitCode(Process process, string messageToShow, string result) {
_logger.LogDebug($"Process exit code: {process.ExitCode}");
if ( process.ExitCode != 0 ) {
return CommandResult.Fail($"(exit code: {process.ExitCode}) {messageToShow}");
}
return CommandResult.Success(messageToShow, result);
}

CommandResult CheckCommandResult(string errorRegex, string messageToCheck, string messageToShow, bool checkResultValue, string result) {
return
ContainsError(errorRegex, messageToCheck) || (checkResultValue && string.IsNullOrEmpty(result)) ?
Expand Down
2 changes: 1 addition & 1 deletion Server/Server.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.28.55.0</Version>
<Version>0.29.56.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
Expand Down

0 comments on commit 6b92051

Please sign in to comment.