-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLine.cs
83 lines (69 loc) · 2.95 KB
/
CommandLine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace GitHooksCsharp.Cmd;
/// <summary>
/// Provides methods to execute commands using PowerShell.
/// </summary>
public static class CommandLine
{
// TODO: Allow the user to specify the CLI to use.
private readonly static string Cli = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "powershell.exe" : "/bin/bash";
/// <summary>
/// Executes a given command using PowerShell and returns the result.
/// </summary>
/// <param name="command">The command to execute.</param>
/// <param name="path"></param>
/// <returns>A <see cref="CmdResult"/> containing the standard output, exit code, and error output.</returns>
public static CmdResult Execute(string command, string path = "")
{
command = $"{command} {path}";
command = command.Replace("\"", "\"\"").Trim();
using Process proc = new();
proc.StartInfo = new ProcessStartInfo
{
FileName = Cli,
Arguments = "-c \"" + command + "\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
};
proc.Start();
proc.WaitForExit();
string stdResult = proc.StandardOutput.ReadToEnd().Trim();
string errResult = proc.StandardError.ReadToEnd().Trim();
int exitCode = proc.ExitCode;
if (exitCode != 0 && string.IsNullOrEmpty(errResult))
errResult = stdResult;
return new CmdResult(stdResult, exitCode, errResult, path);
}
/// <summary>
/// Executes a given command asynchronously using PowerShell and returns the result.
/// </summary>
/// <param name="command">The command to execute.</param>
/// <param name="path">The path to execute the command in. Default is an empty string.</param>
/// <returns>A <see cref="CmdResult"/> containing the standard output, exit code, and error output.</returns>
public static async Task<CmdResult> ExecuteAsync(string command, string path = "")
{
command = $"{command} {path}";
command = command.Replace("\"", "\"\"").Trim();
using Process proc = new();
proc.StartInfo = new ProcessStartInfo
{
FileName = Cli,
Arguments = "-c \"" + command + "\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
};
proc.Start();
await proc.WaitForExitAsync();
string stdResult = await proc.StandardOutput.ReadToEndAsync();
string errResult = await proc.StandardError.ReadToEndAsync();
int exitCode = proc.ExitCode;
if (exitCode != 0 && string.IsNullOrEmpty(errResult))
errResult = stdResult;
return new CmdResult(stdResult.Trim(), exitCode, errResult.Trim(), path);
}
}