diff --git a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs index d5b22780c..471be80b9 100644 --- a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs +++ b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using System.Net.Http; +using CommandLine; using FilterLists.Agent.AppSettings; using FilterLists.Agent.Core.Interfaces; using FilterLists.Agent.Infrastructure.Clients; @@ -17,6 +18,7 @@ public static void RegisterAgentServices(this IServiceCollection services) { services.AddConfiguration(); services.AddLoggingCustom(); + services.AddTransient(); services.AddMediatR(typeof(Program).Assembly); services.AddAgentHttpClient(); services.AddSingleton(); @@ -39,15 +41,6 @@ private static void AddConfiguration(this IServiceCollection services) services.Configure(config.GetSection(nameof(GitHub))); } - private static void AddAgentHttpClient(this IServiceCollection services) - { - services.AddHttpClient().ConfigureHttpMessageHandlerBuilder(b => - { - b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false}; - b.Build(); - }); - } - private static void AddLoggingCustom(this IServiceCollection services) { services.AddLogging(b => @@ -57,5 +50,14 @@ private static void AddLoggingCustom(this IServiceCollection services) b.AddApplicationInsights(appInsightsConfig.Value.InstrumentationKey); }); } + + private static void AddAgentHttpClient(this IServiceCollection services) + { + services.AddHttpClient().ConfigureHttpMessageHandlerBuilder(b => + { + b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false}; + b.Build(); + }); + } } } \ No newline at end of file diff --git a/src/FilterLists.Agent/FilterLists.Agent.csproj b/src/FilterLists.Agent/FilterLists.Agent.csproj index 9701f9c18..dc05cae08 100644 --- a/src/FilterLists.Agent/FilterLists.Agent.csproj +++ b/src/FilterLists.Agent/FilterLists.Agent.csproj @@ -27,6 +27,7 @@ + diff --git a/src/FilterLists.Agent/Options.cs b/src/FilterLists.Agent/Options.cs new file mode 100644 index 000000000..fc683e2da --- /dev/null +++ b/src/FilterLists.Agent/Options.cs @@ -0,0 +1,21 @@ +using CommandLine; +using JetBrains.Annotations; + +namespace FilterLists.Agent +{ + [UsedImplicitly] + public class Options + { + public Options(bool captureLists, bool validateUrls) + { + CaptureLists = captureLists; + ValidateUrls = validateUrls; + } + + [Option('c', "capture", Required = false, HelpText = "Capture copies of all lists in the archives repository.")] + public bool CaptureLists { get; } + + [Option('v', "validate", Required = false, HelpText = "Validate all URLs in the FilterLists database.")] + public bool ValidateUrls { get; } + } +} \ No newline at end of file diff --git a/src/FilterLists.Agent/Program.cs b/src/FilterLists.Agent/Program.cs index 34f2daa20..3d13a53d7 100644 --- a/src/FilterLists.Agent/Program.cs +++ b/src/FilterLists.Agent/Program.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using CommandLine; using FilterLists.Agent.Extensions; using FilterLists.Agent.Features.Lists; using FilterLists.Agent.Features.Urls; @@ -12,12 +13,21 @@ public static class Program { private static IServiceProvider _serviceProvider; - public static async Task Main() + public static async Task Main(string[] args) { BuildServiceProvider(); + var parser = _serviceProvider.GetService(); var mediator = _serviceProvider.GetService(); - await mediator.Send(new CaptureLists.Command()); - await mediator.Send(new ValidateAllUrls.Command()); + + await parser.ParseArguments(args).MapResult(async o => + { + if (o.CaptureLists) + await mediator.Send(new CaptureLists.Command()); + if (o.ValidateUrls) + await mediator.Send(new ValidateAllUrls.Command()); + }, + e => Task.FromResult(0) + ); } private static void BuildServiceProvider() diff --git a/src/FilterLists.Agent/Properties/launchSettings.json b/src/FilterLists.Agent/Properties/launchSettings.json new file mode 100644 index 000000000..738b9cac6 --- /dev/null +++ b/src/FilterLists.Agent/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "FilterLists.Agent": { + "commandName": "Project", + "commandLineArgs": "-c -v" + } + } +} \ No newline at end of file