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