diff --git a/src/ConductorSharp.Toolkit/Filters/ITaskFilter.cs b/src/ConductorSharp.Toolkit/Filters/ITaskFilter.cs new file mode 100644 index 00000000..6b562495 --- /dev/null +++ b/src/ConductorSharp.Toolkit/Filters/ITaskFilter.cs @@ -0,0 +1,9 @@ +using ConductorSharp.Client.Model.Common; + +namespace ConductorSharp.Toolkit.Filters +{ + public interface ITaskFilter + { + bool Test(TaskDefinition taskDefinition); + } +} diff --git a/src/ConductorSharp.Toolkit/Filters/IWorkflowFilter.cs b/src/ConductorSharp.Toolkit/Filters/IWorkflowFilter.cs new file mode 100644 index 00000000..6afdd7da --- /dev/null +++ b/src/ConductorSharp.Toolkit/Filters/IWorkflowFilter.cs @@ -0,0 +1,9 @@ +using ConductorSharp.Client.Model.Common; + +namespace ConductorSharp.Toolkit.Filters +{ + public interface IWorkflowFilter + { + bool Test(WorkflowDefinition workflowDefinition); + } +} diff --git a/src/ConductorSharp.Toolkit/Filters/NameTaskFilter.cs b/src/ConductorSharp.Toolkit/Filters/NameTaskFilter.cs new file mode 100644 index 00000000..a04bd415 --- /dev/null +++ b/src/ConductorSharp.Toolkit/Filters/NameTaskFilter.cs @@ -0,0 +1,15 @@ +using ConductorSharp.Client.Model.Common; +using ConductorSharp.Toolkit.Util; +using System.Text.RegularExpressions; + +namespace ConductorSharp.Toolkit.Filters +{ + public class NameTaskFilter : ITaskFilter + { + private readonly Regex[] _regexes; + + public NameTaskFilter(IEnumerable names) => _regexes = names.Select(name => RegexUtil.CreateNameRegex(name)).ToArray(); + + public bool Test(TaskDefinition taskDefinition) => _regexes.Any(regex => regex.IsMatch(taskDefinition.Name)); + } +} diff --git a/src/ConductorSharp.Toolkit/Filters/NameWorkflowFilter.cs b/src/ConductorSharp.Toolkit/Filters/NameWorkflowFilter.cs new file mode 100644 index 00000000..fe9189e6 --- /dev/null +++ b/src/ConductorSharp.Toolkit/Filters/NameWorkflowFilter.cs @@ -0,0 +1,15 @@ +using ConductorSharp.Client.Model.Common; +using ConductorSharp.Toolkit.Util; +using System.Text.RegularExpressions; + +namespace ConductorSharp.Toolkit.Filters +{ + public class NameWorkflowFilter : IWorkflowFilter + { + private readonly Regex[] _regexes; + + public NameWorkflowFilter(IEnumerable names) => _regexes = names.Select(name => RegexUtil.CreateNameRegex(name)).ToArray(); + + public bool Test(WorkflowDefinition workflowDefinition) => _regexes.Any(regex => regex.IsMatch(workflowDefinition.Name)); + } +} diff --git a/src/ConductorSharp.Toolkit/Filters/OwnerAppTaskFilter.cs b/src/ConductorSharp.Toolkit/Filters/OwnerAppTaskFilter.cs new file mode 100644 index 00000000..7096ef30 --- /dev/null +++ b/src/ConductorSharp.Toolkit/Filters/OwnerAppTaskFilter.cs @@ -0,0 +1,13 @@ +using ConductorSharp.Client.Model.Common; + +namespace ConductorSharp.Toolkit.Filters +{ + public class OwnerAppTaskFilter : ITaskFilter + { + private readonly string[] _ownerApps; + + public OwnerAppTaskFilter(IEnumerable ownerApps) => _ownerApps = ownerApps.ToArray(); + + public bool Test(TaskDefinition taskDefinition) => taskDefinition.OwnerApp != null && _ownerApps.Any(app => app == taskDefinition.OwnerApp); + } +} diff --git a/src/ConductorSharp.Toolkit/Filters/OwnerAppWorkflowFilter.cs b/src/ConductorSharp.Toolkit/Filters/OwnerAppWorkflowFilter.cs new file mode 100644 index 00000000..84f9adad --- /dev/null +++ b/src/ConductorSharp.Toolkit/Filters/OwnerAppWorkflowFilter.cs @@ -0,0 +1,14 @@ +using ConductorSharp.Client.Model.Common; + +namespace ConductorSharp.Toolkit.Filters +{ + public class OwnerAppWorkflowFilter : IWorkflowFilter + { + private readonly string[] _ownerApps; + + public OwnerAppWorkflowFilter(IEnumerable ownerApps) => _ownerApps = ownerApps.ToArray(); + + public bool Test(WorkflowDefinition workflowDefinition) => + workflowDefinition.OwnerApp != null && _ownerApps.Any(app => app == workflowDefinition.OwnerApp); + } +} diff --git a/src/ConductorSharp.Toolkit/Filters/OwnerEmailTaskFilter.cs b/src/ConductorSharp.Toolkit/Filters/OwnerEmailTaskFilter.cs new file mode 100644 index 00000000..f43f99be --- /dev/null +++ b/src/ConductorSharp.Toolkit/Filters/OwnerEmailTaskFilter.cs @@ -0,0 +1,14 @@ +using ConductorSharp.Client.Model.Common; + +namespace ConductorSharp.Toolkit.Filters +{ + public class OwnerEmailTaskFilter : ITaskFilter + { + private readonly string[] _ownerEmails; + + public OwnerEmailTaskFilter(IEnumerable ownerEmails) => _ownerEmails = ownerEmails.ToArray(); + + public bool Test(TaskDefinition taskDefinition) => + taskDefinition.OwnerEmail != null && _ownerEmails.Any(email => email == taskDefinition.OwnerEmail); + } +} diff --git a/src/ConductorSharp.Toolkit/Filters/OwnerEmailWorkflowFilter.cs b/src/ConductorSharp.Toolkit/Filters/OwnerEmailWorkflowFilter.cs new file mode 100644 index 00000000..35c4d153 --- /dev/null +++ b/src/ConductorSharp.Toolkit/Filters/OwnerEmailWorkflowFilter.cs @@ -0,0 +1,14 @@ +using ConductorSharp.Client.Model.Common; + +namespace ConductorSharp.Toolkit.Filters +{ + public class OwnerEmailWorkflowFilter : IWorkflowFilter + { + private readonly string[] _ownerEmails; + + public OwnerEmailWorkflowFilter(IEnumerable ownerEmails) => _ownerEmails = ownerEmails.ToArray(); + + public bool Test(WorkflowDefinition workflowDefinition) => + workflowDefinition.OwnerEmail != null && _ownerEmails.Any(email => email == workflowDefinition.OwnerEmail); + } +} diff --git a/src/ConductorSharp.Toolkit/Models/ToolkitOptions.cs b/src/ConductorSharp.Toolkit/Models/ToolkitOptions.cs index 25a6634e..c1e9d824 100644 --- a/src/ConductorSharp.Toolkit/Models/ToolkitOptions.cs +++ b/src/ConductorSharp.Toolkit/Models/ToolkitOptions.cs @@ -6,5 +6,23 @@ public class ToolkitOptions { [Option('f', "file", HelpText = "Configuration file", Default = "conductorsharp.yaml")] public string ConfigurationFilePath { get; set; } + + [Option('n', "name", HelpText = "Specifies names of the tasks and workflows to scaffold")] + public IEnumerable NameFilters { get; set; } + + [Option('a', "app", HelpText = "Specifies owner apps of the tasks and workflows to scaffold")] + public IEnumerable OwnerAppFilters { get; set; } + + [Option('e', "email", HelpText = "Specifies owner emails of the tasks and workflows to scaffold")] + public IEnumerable OwnerEmailFilters { get; set; } + + [Option("no-tasks", Default = false, SetName = "no-tasks", HelpText = "Skip task scaffolding")] + public bool IgnoreTasks { get; set; } + + [Option("no-workflows", Default = false, SetName = "no-workflows", HelpText = "Skip workflow scaffolding")] + public bool IgnoreWorkflows { get; set; } + + [Option("dry-run", Default = false, HelpText = "Print which workflows would be scaffolded")] + public bool DryRun { get; set; } } } diff --git a/src/ConductorSharp.Toolkit/Program.cs b/src/ConductorSharp.Toolkit/Program.cs index f25f91bb..c9b98954 100644 --- a/src/ConductorSharp.Toolkit/Program.cs +++ b/src/ConductorSharp.Toolkit/Program.cs @@ -61,7 +61,7 @@ private static async Task RunToolkit(ToolkitOptions options) if (!ValidateConfiguration(config)) return; - var container = BuildContainer(config); + var container = BuildContainer(config, options); var commandRegistry = container.Resolve(); // Currently only scaffolding is supported var command = commandRegistry.Get("scaffold"); @@ -104,7 +104,7 @@ private static bool ValidateConfiguration(Configuration config) return validConfiguration; } - private static IContainer BuildContainer(Configuration config) + private static IContainer BuildContainer(Configuration config, ToolkitOptions options) { var serviceCollection = new ServiceCollection(); @@ -118,6 +118,12 @@ private static IContainer BuildContainer(Configuration config) scaffoldingConfig.BaseUrl = config.BaseUrl; scaffoldingConfig.BaseNamespace = config.Namespace; scaffoldingConfig.Destination = config.Destination; + scaffoldingConfig.NameFilters = options.NameFilters.ToArray(); + scaffoldingConfig.OwnerAppFilters = options.OwnerAppFilters.ToArray(); + scaffoldingConfig.OwnerEmailFilters = options.OwnerEmailFilters.ToArray(); + scaffoldingConfig.IgnoreTasks = options.IgnoreTasks; + scaffoldingConfig.IgnoreWorkflows = options.IgnoreWorkflows; + scaffoldingConfig.DryRun = options.DryRun; }); builder.Populate(serviceCollection); diff --git a/src/ConductorSharp.Toolkit/ScaffoldingConfig.cs b/src/ConductorSharp.Toolkit/ScaffoldingConfig.cs index 3cc20542..565e96da 100644 --- a/src/ConductorSharp.Toolkit/ScaffoldingConfig.cs +++ b/src/ConductorSharp.Toolkit/ScaffoldingConfig.cs @@ -5,7 +5,12 @@ public class ScaffoldingConfig public string BaseNamespace { get; set; } public string ApiUrl { get; set; } public string BaseUrl { get; set; } - public bool Dryrun { get; set; } + public bool DryRun { get; set; } public string Destination { get; set; } + public string[] NameFilters { get; set; } + public string[] OwnerAppFilters { get; set; } + public string[] OwnerEmailFilters { get; set; } + public bool IgnoreTasks { get; set; } + public bool IgnoreWorkflows { get; set; } } } diff --git a/src/ConductorSharp.Toolkit/Service/ScaffoldingService.cs b/src/ConductorSharp.Toolkit/Service/ScaffoldingService.cs index 36240c95..d2debe82 100644 --- a/src/ConductorSharp.Toolkit/Service/ScaffoldingService.cs +++ b/src/ConductorSharp.Toolkit/Service/ScaffoldingService.cs @@ -1,6 +1,7 @@ using ConductorSharp.Client.Model.Common; using ConductorSharp.Client.Service; using ConductorSharp.Engine.Util; +using ConductorSharp.Toolkit.Filters; using ConductorSharp.Toolkit.Util; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -28,31 +29,50 @@ public ScaffoldingService(IMetadataService metadataService, IOptions(); + if (_config.NameFilters.Length != 0) + taskFilters.Add(new NameTaskFilter(_config.NameFilters)); + if (_config.OwnerAppFilters.Length != 0) + taskFilters.Add(new OwnerAppTaskFilter(_config.OwnerAppFilters)); + if (_config.OwnerEmailFilters.Length != 0) + taskFilters.Add(new OwnerEmailTaskFilter(_config.OwnerEmailFilters)); + + return taskFilters.ToArray(); + } + + private IWorkflowFilter[] CreateWorkflowFilters() + { + var workflowFilters = new List(); + if (_config.NameFilters.Length != 0) + workflowFilters.Add(new NameWorkflowFilter(_config.NameFilters)); + if (_config.OwnerAppFilters.Length != 0) + workflowFilters.Add(new OwnerAppWorkflowFilter(_config.OwnerAppFilters)); + if (_config.OwnerEmailFilters.Length != 0) + workflowFilters.Add(new OwnerEmailWorkflowFilter(_config.OwnerEmailFilters)); + + return workflowFilters.ToArray(); + } + + // If filter list is empty then all workflows/tasks are returned + + private IEnumerable Filter(IEnumerable workflows, IWorkflowFilter[] filters) => + workflows.Where(wf => filters.All(filter => filter.Test(wf))); + + private IEnumerable Filter(IEnumerable tasks, ITaskFilter[] filters) => + tasks.Where(task => filters.All(filter => filter.Test(task))); } } diff --git a/src/ConductorSharp.Toolkit/Util/RegexUtil.cs b/src/ConductorSharp.Toolkit/Util/RegexUtil.cs new file mode 100644 index 00000000..8e17af91 --- /dev/null +++ b/src/ConductorSharp.Toolkit/Util/RegexUtil.cs @@ -0,0 +1,9 @@ +using System.Text.RegularExpressions; + +namespace ConductorSharp.Toolkit.Util +{ + public static class RegexUtil + { + public static Regex CreateNameRegex(string name) => new($"^{Regex.Escape(name).Replace("%", ".*")}$", RegexOptions.Compiled); + } +}