public ArgumentCollection Map(ArgumentCollection arguments) { if (!arguments.HasArgument(WinServiceWatcherResultArgs.ServiceName) || !(arguments[WinServiceWatcherResultArgs.ServiceName] is string)) { return(null); } return(ArgumentCollection.New() .WithArgument(WinServiceActionExecutionArgs.ServiceName, arguments.GetValue <string>(WinServiceWatcherResultArgs.ServiceName))); }
protected override IEnumerable <System.Diagnostics.Process> GetProcesses(ArgumentCollection arguments) { string processName = ProcessName; if (string.IsNullOrWhiteSpace(processName)) { processName = arguments.GetValue <string>(KillProcessByNameActionExecutionArgs.ProcessName); } if (string.IsNullOrWhiteSpace(processName)) { throw new MissingArgumentException(KillProcessByNameActionExecutionArgs.ProcessName); } LoggingService.Trace($"Getting process by name {processName}"); return(System.Diagnostics.Process.GetProcessesByName(processName)); }
protected override IEnumerable <System.Diagnostics.Process> GetProcesses(ArgumentCollection arguments) { int processId = default; if (arguments.HasArgument(KillProcessByIdActionExecutionArgs.ProcessId)) { processId = arguments.GetValue <int>(KillProcessByIdActionExecutionArgs.ProcessId); } if (processId == default) { throw new MissingArgumentException(KillProcessByIdActionExecutionArgs.ProcessId); } var process = System.Diagnostics.Process.GetProcessById(processId); yield return(process); }
public ActionResult Execute(ArgumentCollection arguments) { ArgumentCollection resultArgumentCollection = ArgumentCollection.New(); try { #region process path var processPath = arguments.GetValue <string>(StartProcessActionExecutionArgs.ProcessPath); if (string.IsNullOrWhiteSpace(processPath)) { processPath = ProcessPath; } if (string.IsNullOrWhiteSpace(processPath)) { throw new MissingArgumentException(StartProcessActionArgs.ProcessPath); } processPath = Environment.ExpandEnvironmentVariables(processPath); if (Path.IsPathRooted(processPath) && !File.Exists(processPath)) { throw new Exception($"The file set as process path doesn't exist, {processPath}"); } #endregion #region process working directory var processWorkingDirectory = arguments.GetValue <string>(StartProcessActionExecutionArgs.ProcessWorkingDirectory); if (string.IsNullOrWhiteSpace(processWorkingDirectory)) { processWorkingDirectory = ProcessWorkingDirectory; } if (Path.IsPathRooted(processPath) && string.IsNullOrWhiteSpace(processWorkingDirectory)) { processWorkingDirectory = Path.GetDirectoryName(processPath); } //if (string.IsNullOrWhiteSpace(processWorkingDirectory)) // throw new MissingArgumentException(StartProcessActionArgs.ProcessWorkingDirectory); #endregion #region process arguments var processArguments = arguments.GetValue <string>(StartProcessActionExecutionArgs.ProcessArguments); if (string.IsNullOrWhiteSpace(processArguments)) { processArguments = ProcessArguments; } #endregion System.Diagnostics.Process process = new System.Diagnostics.Process { StartInfo = { FileName = processPath, Arguments = processArguments, CreateNoWindow = true, } }; if (!string.IsNullOrWhiteSpace(processWorkingDirectory)) { process.StartInfo.WorkingDirectory = processWorkingDirectory; } resultArgumentCollection = ArgumentCollection.New() .WithArgument(StartProcessActionResultArgs.ProcessPath, processPath) .WithArgument(StartProcessActionResultArgs.ProcessArguments, processArguments) .WithArgument(StartProcessActionResultArgs.ProcessWorkingDirectory, processWorkingDirectory); if (!process.Start()) { resultArgumentCollection.Add(StartProcessActionResultArgs.ProcessStarted, false); return(ActionResult.Failed().WithAdditionInformation(resultArgumentCollection)); } if (WaitForExit) { var waitResult = process.WaitForExit(WaitForExitTimeout); resultArgumentCollection.Add(StartProcessActionResultArgs.ProcessStarted, true); resultArgumentCollection.Add(StartProcessActionResultArgs.ProcessExited, waitResult); return(ActionResult.Succeeded().WithAdditionInformation(resultArgumentCollection)); } resultArgumentCollection.Add(StartProcessActionResultArgs.ProcessStarted, true); return(ActionResult.Succeeded().WithAdditionInformation(resultArgumentCollection)); } catch (Exception exception) { LoggingService.Error(exception); return(ActionResult.Failed().WithAdditionInformation(resultArgumentCollection)); } }
public ActionResult Execute(ArgumentCollection arguments) { string filePath = null; try { if (arguments.HasArgument(RenameFileActionArgs.SourceFilePath)) { if (arguments[RenameFileActionArgs.SourceFilePath] is List <string> castedFilePaths) { if (castedFilePaths.Count() > 1) { throw new Exception($"Operation not allowed: Got multiple files to be renamed."); } if (!castedFilePaths.Any()) { throw new Exception($"Operation not allowed: Got empty list of files to be renamed."); } filePath = castedFilePaths.Single(); } else { filePath = arguments.GetValue <string>(RenameFileActionArgs.SourceFilePath); } } else { if (!string.IsNullOrWhiteSpace(SourceFilePath)) { filePath = SourceFilePath; } } if (string.IsNullOrWhiteSpace(filePath)) { throw new MissingArgumentException(RenameFileActionArgs.SourceFilePath); } if (!Path.IsPathRooted(filePath)) { throw new FormatException($"Source file has incorrect format, expected Rooted Path: {filePath}"); } // Files must exist if (!File.Exists(filePath)) { throw new FileNotFoundException($"File not found {filePath}"); } if (string.IsNullOrWhiteSpace(DestinationFileName) && string.IsNullOrWhiteSpace(DestinationPrefix) && string.IsNullOrWhiteSpace(DestinationExtension)) { throw new MissingArgumentException(RenameFileActionArgs.DestinationFileName); } var directory = Path.GetDirectoryName(filePath); if (string.IsNullOrWhiteSpace(directory)) { throw new Exception($"Unable to get directory name from file {filePath}"); } var fileName = Path.GetFileName(filePath); if (!string.IsNullOrWhiteSpace(DestinationFileName)) { fileName = DestinationFileName; } else { if (!string.IsNullOrEmpty(DestinationExtension)) { fileName = Path.ChangeExtension(fileName, DestinationExtension); } if (!string.IsNullOrEmpty(DestinationPrefix)) { fileName = DestinationPrefix + fileName; } } if (string.IsNullOrWhiteSpace(fileName)) { throw new Exception($"FileName not resolved, Cannot rename file"); } var destinationPath = Path.Combine(directory, fileName); if (File.Exists(destinationPath)) { throw new Exception($"Another file with the same name already exists {destinationPath}"); } File.Move(filePath, destinationPath); LoggingService.Trace($"File ({filePath}) renamed successfully to path ({destinationPath})"); return(ActionResult.Succeeded().WithAdditionInformation(ArgumentCollection.New() .WithArgument(RenameFileActionResultArgs.SourceFilePath, filePath) .WithArgument(RenameFileActionResultArgs.DestinationFilePath, destinationPath) )); } catch (Exception exception) { LoggingService.Error(exception); return(new ActionResult(false) { AttachedException = exception, AdditionalInformation = ArgumentCollection.New().WithArgument(RenameFileActionResultArgs.SourceFilePath, filePath) }); } }
/// <summary> /// Executes the specified arguments. /// </summary> /// <param name="arguments">The arguments.</param> /// <returns></returns> /// <exception cref="Exception"> /// Missing mandatory execution argument: {GenerateQRCodeActionExecutionArgs.PlainText} /// or /// Mandatory instance argument is not set : {GenerateQRCodeActionInstanceArgs.ErrorCorrectionLevel} /// or /// Wrong value for argument: {GenerateQRCodeActionInstanceArgs.PixelPerModule} set to {PixelPerModule} /// or /// Mandatory instance argument is not set : {GenerateQRCodeActionInstanceArgs.ImageFormat} /// or /// Missing mandatory argument: {GenerateQRCodeActionExecutionArgs.OutputFilePath} /// or /// Another file with the same name exist: {outputFilePath} /// or /// Failed to get the QR Code image. /// </exception> public ActionResult Execute(ArgumentCollection arguments) { var plainText = string.Empty; var outputFilePath = OutputFilePath; try { if (arguments.HasArgument(GenerateQRCodeActionExecutionArgs.PlainText)) { plainText = arguments.GetValue <string>(GenerateQRCodeActionExecutionArgs.PlainText); } if (string.IsNullOrWhiteSpace(plainText)) { throw new Exception( $"Missing mandatory execution argument: {GenerateQRCodeActionExecutionArgs.PlainText}"); } QRCodeGenerator.ECCLevel?errorCorrectLevel = null; if (!string.IsNullOrWhiteSpace(ErrorCorrectLevel)) { if (Enum.TryParse <QRCodeGenerator.ECCLevel>(ErrorCorrectLevel, true, out var eccl)) { errorCorrectLevel = eccl; } } if (!errorCorrectLevel.HasValue) { throw new Exception( $"Mandatory instance argument is not set : {GenerateQRCodeActionInstanceArgs.ErrorCorrectionLevel}"); } var pixelPerModule = 0; if (PixelPerModule >= MinPixelPerModule && PixelPerModule <= MaxPixelPerModule) { pixelPerModule = PixelPerModule; } if (pixelPerModule == 0) { throw new Exception( $"Wrong value for argument: {GenerateQRCodeActionInstanceArgs.PixelPerModule} set to {PixelPerModule}"); } string imageFormat = null; if (!string.IsNullOrWhiteSpace(ImageFormat) && AllowedImageFormats() .Any(f => string.Equals(f, ImageFormat, StringComparison.CurrentCultureIgnoreCase))) { imageFormat = ImageFormat; } if (imageFormat == null) { throw new Exception( $"Mandatory instance argument is not set : {GenerateQRCodeActionInstanceArgs.ImageFormat}"); } if (string.IsNullOrWhiteSpace(outputFilePath)) { if (arguments.HasArgument(GenerateQRCodeActionExecutionArgs.OutputFilePath)) { outputFilePath = arguments.GetValue <string>(GenerateQRCodeActionExecutionArgs.OutputFilePath); } } if (string.IsNullOrWhiteSpace(outputFilePath)) { throw new Exception( $"Missing mandatory argument: {GenerateQRCodeActionExecutionArgs.OutputFilePath}"); } if (EraseExistingFile) { if (File.Exists(outputFilePath)) { File.Delete(outputFilePath); } } if (File.Exists(outputFilePath)) { throw new Exception($"Another file with the same name exist: {outputFilePath}"); } byte[] qrCodeBytes = null; using (var qrCodeGenerator = new QRCodeGenerator()) { var qrCodeData = qrCodeGenerator.CreateQrCode(plainText, errorCorrectLevel.Value, ForceUtf8); if (qrCodeData != null) { qrCodeBytes = GetByteQrCode(imageFormat, pixelPerModule, qrCodeData); } } if (qrCodeBytes == null) { throw new Exception("Failed to get the QR Code image."); } File.WriteAllBytes(outputFilePath, qrCodeBytes); // qrCodeImage.Save(outputFilePath, imageFormat); return(ActionResult.Succeeded().WithAdditionInformation(ArgumentCollection.New() .WithArgument(GenerateQRCodeActionResultsArgs.OutputFilePath, outputFilePath) .WithArgument(GenerateQRCodeActionResultsArgs.QRCodeData, plainText) )); } catch (Exception exception) { LoggingService.Error(exception); return(ActionResult.Failed(exception).WithAdditionInformation(ArgumentCollection.New() .WithArgument(GenerateQRCodeActionResultsArgs.OutputFilePath, outputFilePath) .WithArgument(GenerateQRCodeActionResultsArgs.QRCodeData, plainText) )); } }
public ActionResult Execute(ArgumentCollection arguments) { string outputDirPath = null; string sourceZipPath = null; try { if (arguments.HasArgument(UnzipArchiveActionExecutionArgs.OutputDirPath)) { outputDirPath = arguments.GetValue <string>(UnzipArchiveActionExecutionArgs.OutputDirPath); } if (arguments.HasArgument(UnzipArchiveActionExecutionArgs.SourceZipPath)) { sourceZipPath = arguments.GetValue <string>(UnzipArchiveActionExecutionArgs.SourceZipPath); } if (string.IsNullOrWhiteSpace(outputDirPath)) { if (!string.IsNullOrWhiteSpace(OutputDirPath)) { outputDirPath = OutputDirPath; } } if (string.IsNullOrWhiteSpace(sourceZipPath)) { if (!string.IsNullOrWhiteSpace(SourceZipPath)) { sourceZipPath = SourceZipPath; } } if (string.IsNullOrWhiteSpace(sourceZipPath)) { throw new MissingArgumentException(UnzipArchiveActionExecutionArgs.SourceZipPath); } if (string.IsNullOrWhiteSpace(outputDirPath)) { throw new MissingArgumentException(UnzipArchiveActionExecutionArgs.OutputDirPath); } var result = ZipUtilities.ExtractArchive(sourceZipPath, outputDirPath, OverrideFiles); return(result ? ActionResult.Succeeded().WithAdditionInformation(ArgumentCollection.New() .WithArgument(UnzipArchiveActionResultsArgs.OutputDirPath, outputDirPath) .WithArgument(UnzipArchiveActionResultsArgs.SourceZipPath, sourceZipPath) ) : ActionResult.Failed().WithAdditionInformation(ArgumentCollection.New() .WithArgument(UnzipArchiveActionResultsArgs.OutputDirPath, outputDirPath) .WithArgument(UnzipArchiveActionResultsArgs.SourceZipPath, sourceZipPath))); } catch (Exception exception) { return(ActionResult.Failed().WithException(exception).WithAdditionInformation(ArgumentCollection.New() .WithArgument(UnzipArchiveActionResultsArgs.OutputDirPath, outputDirPath) .WithArgument(UnzipArchiveActionResultsArgs.SourceZipPath, sourceZipPath))); } }
public ActionResult Execute(ArgumentCollection arguments) { string serviceName = null; try { if (arguments.HasArgument(WinServiceActionExecutionArgs.ServiceName)) { serviceName = arguments.GetValue <string>(WinServiceActionExecutionArgs.ServiceName); } if (serviceName == null && !string.IsNullOrWhiteSpace(ServiceName)) { serviceName = ServiceName; } if (string.IsNullOrWhiteSpace(serviceName)) { throw new Exception($"Missing Argument {WinServiceActionInstanceArgs.ServiceName}"); } if (!TryGetTargetStatus(out ServiceTargetStatus status)) { throw new Exception($"Unexpected Target Status Value ({Status}) Expected valid casting from ({typeof(ServiceTargetStatus)})"); } bool result = false; switch (status) { case ServiceTargetStatus.Start: { result = WinServiceUtilities.StartService(serviceName, Wait, WaitTimeout); break; } case ServiceTargetStatus.Stop: { result = WinServiceUtilities.StopService(serviceName, Wait, WaitTimeout); break; } case ServiceTargetStatus.Pause: { result = WinServiceUtilities.PauseService(serviceName, Wait, WaitTimeout); break; } case ServiceTargetStatus.Resume: { result = WinServiceUtilities.ResumeService(serviceName, Wait, WaitTimeout); break; } default: { throw new Exception("Unexpected ControlStatus when switching Control cases"); } } return(result ? ActionResult.Succeeded().WithAdditionInformation(ArgumentCollection.New() .WithArgument(WinServiceActionResultsArgs.ServiceName, serviceName) .WithArgument(WinServiceActionResultsArgs.ActionTargetStatus, Status) ) : ActionResult.Failed().WithAdditionInformation(ArgumentCollection.New() .WithArgument(WinServiceActionResultsArgs.ServiceName, serviceName) .WithArgument(WinServiceActionResultsArgs.ActionTargetStatus, Status) )); } catch (Exception exception) { LoggingService.Error(exception); return(ActionResult.Failed(exception).WithAdditionInformation(ArgumentCollection.New() .WithArgument(WinServiceActionResultsArgs.ServiceName, serviceName) .WithArgument(WinServiceActionResultsArgs.ActionTargetStatus, Status) )); } }
public ActionResult Execute(ArgumentCollection arguments) { List <string> files = null; try { if (arguments.HasArgument(ZipFilesActionExecutionArgs.FilesPaths)) { if (!(arguments[ZipFilesActionExecutionArgs.FilesPaths] is List <string> filePaths)) { throw new ArgumentsValidationException( $"unable to cast argument value into list of string. key({ZipFilesActionExecutionArgs.FilesPaths})"); } files = filePaths; // arguments.GetValue<string[]>(ZipFilesActionExecutionArgs.FilesPaths); } else if (arguments.HasArgument(ZipFilesActionExecutionArgs.FilesInDirectoryPath)) { var directory = arguments.GetValue <string>(ZipFilesActionExecutionArgs.FilesInDirectoryPath); if (Directory.Exists(directory)) { files = Directory.GetFiles(directory).ToList(); } } if (files == null) { if (!string.IsNullOrWhiteSpace(FilePath)) { files = new List <string>() { FilePath }; } else if (!string.IsNullOrWhiteSpace(FilesInDirectoryPath) && Directory.Exists(FilesInDirectoryPath)) { if (Directory.Exists(FilesInDirectoryPath)) { files = Directory.GetFiles(FilesInDirectoryPath).ToList(); } } } if (files == null) { throw new MissingArgumentsException($"Not source files to zip are defined. " + $"at least one of the following arguments must be set [{ZipFilesActionInstanceArgs.FilePath}, {ZipFilesActionInstanceArgs.FilesInDirectoryPath}, {ZipFilesActionExecutionArgs.FilesInDirectoryPath}, {ZipFilesActionExecutionArgs.FilesPaths} ]"); } if (!files.Any()) { LoggingService.Warn($"No files found to zip"); return(ActionResult.Succeeded()); } var missingFiles = files.Where(f => !File.Exists(f)).ToList(); if (!IgnoreMissingFiles && missingFiles.Any()) { throw new Exception( $"Cannot execute action because the following files are not found : ({string.Join(",", missingFiles)})"); } else if (IgnoreMissingFiles && files.Count == 1 && missingFiles.Any()) { throw new Exception( $"Cannot execute action because the unique file to zip is not found ({files.Single()})"); } string zipPath = null; if (arguments.HasArgument(ZipFilesActionExecutionArgs.ArchivePath)) { zipPath = arguments.GetValue <string>(ZipFilesActionExecutionArgs.ArchivePath); } else if (!string.IsNullOrWhiteSpace(ArchivePath)) { zipPath = ArchivePath; } if (string.IsNullOrWhiteSpace(zipPath)) { throw new MissingArgumentsException( $"No archive path was specified in instance or executing arguments." + $" at least one of this arguments must be set Instance[{ZipFilesActionInstanceArgs.ArchivePath}] Execution[{ZipFilesActionExecutionArgs.ArchivePath}]"); } var results = ZipUtilities.AddFilesToArchive(zipPath, files); if (results) { if (DeleteZippedFiles) { LoggingService.Debug($"Deleting ({files.Count}) Zipped files"); foreach (var file in files) { try { LoggingService.Debug($"Deleting Zipped File {file}"); File.Delete(file); LoggingService.Debug($"Zipped File {file} deleted successfully"); } catch (Exception exception) { LoggingService.Error(exception); } } } else if (MoveZippedFiles && !string.IsNullOrWhiteSpace(MoveZippedFilesToPath)) { LoggingService.Debug($"Moving ({files.Count}) Zipped files to path {MoveZippedFilesToPath}"); if (!Directory.Exists(MoveZippedFilesToPath)) { Directory.CreateDirectory(MoveZippedFilesToPath); } foreach (var file in files) { try { var destinationFilePath = Path.Combine(MoveZippedFilesToPath, Path.GetFileName(file)); LoggingService.Debug($"Moving file {file} to {destinationFilePath}"); File.Move(file, destinationFilePath); LoggingService.Debug( $"Zipped file {Path.GetFileName(file)} moved successfully to {MoveZippedFilesToPath}"); } catch (Exception exception) { LoggingService.Error(exception); } } } } return(results? ActionResult.Succeeded().WithAdditionInformation(ArgumentCollection.New() .WithArgument(ZipFilesActionResultArgs.ArchivePath, ArchivePath) .WithArgument(ZipFilesActionResultArgs.ZippedFiles, files) ) : ActionResult.Failed() .WithAdditionInformation(ArgumentCollection.New() .WithArgument(ZipFilesActionResultArgs.ArchivePath, ArchivePath) .WithArgument(ZipFilesActionResultArgs.ZippedFiles, files ?? new List <string>()) ) ); } catch (Exception exception) { return(ActionResult.Failed().WithException(exception) .WithAdditionInformation(ArgumentCollection.New() .WithArgument(ZipFilesActionResultArgs.ArchivePath, ArchivePath) .WithArgument(ZipFilesActionResultArgs.ZippedFiles, files ?? new List <string>()) ) ); } }