Skip to content

Commit

Permalink
feat: update CommandExecutor and add EnsureSuccessExitCode extension
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Jan 3, 2024
1 parent c0c9599 commit 762c688
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
4 changes: 3 additions & 1 deletion samples/DotNetCoreSample/CommandExecutorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public static void MainTest()
.PrintOutputToConsole()
.EnsureSuccessExitCode();

ExecuteAndOutput("hostname");
ExecuteAndOutput("hostname").EnsureSuccessExitCode();

ExecuteAndOutputAsync("hostname").Wait();

ExecuteCommandAndOutput("hostname").EnsureSuccessExitCode();
}
}
15 changes: 15 additions & 0 deletions src/WeihanLi.Common/Extensions/CoreExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,21 @@ public static int Sign(this int value)
return Math.Sign(value);
}

/// <summary>
/// Ensures the exitCode success
/// </summary>
/// <param name="exitCode">exitCode</param>
/// <param name="successCode">successCode</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException">Exception when exitCode not match the successCode</exception>
public static int EnsureSuccessExitCode(this int exitCode, int successCode = 0)
{
if (exitCode != 0)
throw new InvalidOperationException($"Unexpected exit code:{ExitCode}");

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / windows-build

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

Check failure on line 1568 in src/WeihanLi.Common/Extensions/CoreExtension.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

The name 'ExitCode' does not exist in the current context

return exitCode;
}

#endregion int

#region long
Expand Down
59 changes: 48 additions & 11 deletions src/WeihanLi.Common/Helpers/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,25 @@ public static CommandResult ExecuteCommandAndCapture(string command, string? wor
var cmd = command.Split(SpaceSeparator, 2);
return ExecuteAndCapture(cmd[0], cmd.Length > 1 ? cmd[1] : null, workingDirectory, configure);
}


/// <summary>
/// Execute command with a process
/// </summary>
/// <param name="command">executable command and argument</param>
/// <param name="stdout">stdout writer, write to console by default</param>
/// <param name="stderr">stderr writer, write to console by default</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="configure">configure the ProcessStartInfo</param>
/// <returns>exit code</returns>
public static int ExecuteCommandAndOutput(string command,
TextWriter? stdout = null, TextWriter? stderr = null,
string? workingDirectory = null, Action<ProcessStartInfo>? configure = null)
{
Guard.NotNullOrEmpty(command);
var cmd = command.Split(SpaceSeparator, 2);
return ExecuteAndOutput(cmd[0], cmd.Length > 1 ? cmd[1] : null, stdout, stderr, workingDirectory, configure);
}

/// <summary>
/// Execute command async
/// </summary>
Expand Down Expand Up @@ -68,6 +86,26 @@ public static Task<CommandResult> ExecuteCommandAndCaptureAsync(string command,
return ExecuteAndCaptureAsync(cmd[0], cmd.Length > 1 ? cmd[1] : null, workingDirectory, configure, cancellationToken);
}

/// <summary>
/// Execute command with a process
/// </summary>
/// <param name="command">executable command and argument</param>
/// <param name="arguments">command arguments</param>
/// <param name="stdout">stdout writer, write to console by default</param>
/// <param name="stderr">stderr writer, write to console by default</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="configure">configure the ProcessStartInfo</param>
/// <param name="cancellationToken">cancellationToken</param>
/// <returns>exit code</returns>
public static Task<int> ExecuteCommandAndOutputAsync(string command, string? arguments = null,
TextWriter? stdout = null, TextWriter? stderr = null,
string? workingDirectory = null, Action<ProcessStartInfo>? configure = null, CancellationToken cancellationToken = default)
{
Guard.NotNullOrEmpty(command);
var cmd = command.Split(SpaceSeparator, 2);
return ExecuteAndOutputAsync(cmd[0], cmd.Length > 1 ? cmd[1] : null, stdout, stderr, workingDirectory, configure, cancellationToken);
}

/// <summary>
/// Execute command with a process
/// </summary>
Expand Down Expand Up @@ -114,13 +152,14 @@ public static async Task<int> ExecuteAsync(string commandPath, string? arguments
/// </summary>
/// <param name="commandPath">executable command path</param>
/// <param name="arguments">command arguments</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="stdout">stdout writer, write to console by default</param>
/// <param name="stderr">stderr writer, write to console by default</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="configure">configure the ProcessStartInfo</param>
/// <returns>exit code</returns>
public static int ExecuteAndOutput(string commandPath, string? arguments = null, string? workingDirectory = null,
TextWriter? stdout = null, TextWriter? stderr = null, Action<ProcessStartInfo>? configure = null)
public static int ExecuteAndOutput(string commandPath, string? arguments = null,
TextWriter? stdout = null, TextWriter? stderr = null,
string? workingDirectory = null, Action<ProcessStartInfo>? configure = null)
{
var processStartInfo = new ProcessStartInfo(commandPath, arguments ?? string.Empty)
{
Expand All @@ -137,14 +176,15 @@ public static int ExecuteAndOutput(string commandPath, string? arguments = null,
/// </summary>
/// <param name="commandPath">executable command path</param>
/// <param name="arguments">command arguments</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="stdout">stdout writer, write to console by default</param>
/// <param name="stderr">stderr writer, write to console by default</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="configure">configure the ProcessStartInfo</param>
/// <param name="cancellationToken">cancellationToken</param>
/// <returns>exit code</returns>
public static async Task<int> ExecuteAndOutputAsync(string commandPath, string? arguments = null, string? workingDirectory = null,
TextWriter? stdout = null, TextWriter? stderr = null, Action<ProcessStartInfo>? configure = null, CancellationToken cancellationToken = default)
public static async Task<int> ExecuteAndOutputAsync(string commandPath, string? arguments = null,
TextWriter? stdout = null, TextWriter? stderr = null,
string? workingDirectory = null, Action<ProcessStartInfo>? configure = null, CancellationToken cancellationToken = default)
{
var processStartInfo = new ProcessStartInfo(commandPath, arguments ?? string.Empty)
{
Expand Down Expand Up @@ -230,10 +270,7 @@ public sealed class CommandResult(int exitCode, string standardOut, string stand

public CommandResult EnsureSuccessExitCode(int successCode = 0)
{
if (ExitCode != successCode)
{
throw new InvalidOperationException($"Unexpected exit code:{ExitCode}");
}
ExitCode.EnsureSuccessExitCode(successCode);
return this;
}
}

0 comments on commit 762c688

Please sign in to comment.