You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
4.5 KiB
123 lines
4.5 KiB
using CliWrap;
|
|
using CliWrap.Buffered;
|
|
using CoreAgent.Domain.Interfaces;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CoreAgent.Infrastructure.Command;
|
|
|
|
/// <summary>
|
|
/// Windows命令策略实现
|
|
/// </summary>
|
|
public class WindowsCommandStrategy : ICommandStrategy
|
|
{
|
|
private readonly ILogger<WindowsCommandStrategy> _logger;
|
|
|
|
public WindowsCommandStrategy(ILogger<WindowsCommandStrategy> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task ExecuteBufferedAsync(string arguments, Action<string> onOutputReceived, CancellationTokenSource source)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogDebug("start ExecuteAsync cmd :{Arguments}", arguments);
|
|
var cmd = Cli.Wrap("cmd.exe")
|
|
.WithArguments($"/c {arguments}")
|
|
.WithStandardOutputPipe(PipeTarget.ToDelegate(onOutputReceived))
|
|
.WithStandardErrorPipe(PipeTarget.ToDelegate(onOutputReceived))
|
|
.WithValidation(CommandResultValidation.None);
|
|
var result = await cmd.ExecuteBufferedAsync(source.Token);
|
|
_logger.LogInformation("{Arguments} result:{IsSuccess}", arguments, result.IsSuccess);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "ExecuteAsync cmd {Arguments} failed", arguments);
|
|
}
|
|
}
|
|
|
|
public async Task<string> ExecuteBufferedAsync(string arguments, CancellationTokenSource source)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogDebug("start ExecuteAsync cmd :{Arguments}", arguments);
|
|
var cmd = Cli.Wrap("cmd.exe")
|
|
.WithArguments($"/c {arguments}")
|
|
.WithValidation(CommandResultValidation.None);
|
|
var cmdResult = await cmd.ExecuteBufferedAsync(source.Token);
|
|
string result = string.Empty;
|
|
if (cmdResult.IsSuccess)
|
|
result = cmdResult.StandardOutput;
|
|
else
|
|
result = cmdResult.StandardError;
|
|
_logger.LogDebug("ExecuteAsync cmd :{Arguments} StandardOutput:{Output} :StandardError {Error}",
|
|
arguments, cmdResult.StandardOutput, cmdResult.StandardError);
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "ExecuteAsync cmd {Arguments} failed", arguments);
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ExecuteAsync(string arguments, CancellationTokenSource source)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogDebug("start ExecuteAsync cmd :{Arguments}", arguments);
|
|
var cmd = await Cli.Wrap("cmd.exe")
|
|
.WithArguments($"/c {arguments}")
|
|
.ExecuteAsync(source.Token);
|
|
_logger.LogDebug("ExecuteAsync cmd :{Arguments} :result {IsSuccess}", arguments, cmd.IsSuccess);
|
|
return cmd.IsSuccess;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "ExecuteAsync cmd {Arguments} failed", arguments);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<string> ExecuteBufferedAsync(string[] arguments, CancellationTokenSource source, string command = "powershell.exe")
|
|
{
|
|
try
|
|
{
|
|
_logger.LogDebug("start ExecuteAsync cmd :{Arguments}", arguments);
|
|
var cmd = Cli.Wrap(command)
|
|
.WithArguments(arguments)
|
|
.WithValidation(CommandResultValidation.None);
|
|
var cmdResult = await cmd.ExecuteBufferedAsync(source.Token);
|
|
string result = string.Empty;
|
|
if (cmdResult.IsSuccess)
|
|
result = cmdResult.StandardOutput;
|
|
else
|
|
result = cmdResult.StandardError;
|
|
_logger.LogDebug("ExecuteAsync cmd :{Arguments} :result {Result}", arguments, result);
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "ExecuteAsync cmd {Arguments} failed", arguments);
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ExecuteAsync(string[] arguments, CancellationTokenSource source, string command = "powershell.exe")
|
|
{
|
|
try
|
|
{
|
|
_logger.LogDebug("start ExecuteAsync cmd :{Arguments}", arguments);
|
|
var cmd = await Cli.Wrap(command)
|
|
.WithArguments(arguments)
|
|
.WithValidation(CommandResultValidation.None)
|
|
.ExecuteAsync(source.Token);
|
|
return cmd.IsSuccess;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "ExecuteAsync cmd {Arguments} failed", arguments);
|
|
return false;
|
|
}
|
|
}
|
|
}
|