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.

44 lines
1.6 KiB

using CoreAgent.Domain.Interfaces;
using Microsoft.Extensions.Logging;
using System.Runtime.InteropServices;
namespace CoreAgent.Infrastructure.Command;
/// <summary>
/// 命令策略工厂实现
/// </summary>
public class CommandStrategyFactory : ICommandStrategyFactory
{
private readonly ILogger<CommandStrategyFactory> _logger;
private readonly ILoggerFactory _loggerFactory;
public CommandStrategyFactory(ILogger<CommandStrategyFactory> logger, ILoggerFactory loggerFactory)
{
_logger = logger;
_loggerFactory = loggerFactory;
}
/// <summary>
/// 创建适合当前操作系统的命令策略
/// </summary>
/// <returns>命令策略实例</returns>
/// <exception cref="PlatformNotSupportedException">当操作系统不是Windows或Linux时抛出</exception>
public ICommandStrategy CreateStrategy()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_logger.LogInformation("Creating Windows command strategy");
return new WindowsCommandStrategy(_loggerFactory.CreateLogger<WindowsCommandStrategy>());
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
_logger.LogInformation("Creating Linux command strategy");
return new LinuxCommandStrategy(_loggerFactory.CreateLogger<LinuxCommandStrategy>());
}
else
{
_logger.LogError("Unsupported operating system");
throw new PlatformNotSupportedException("Only Windows and Linux are supported.");
}
}
}