support cmd args

This commit is contained in:
Collin M. Barrett 2019-07-04 11:00:17 -05:00
parent 69f1072b9d
commit 76b1e2afb7
5 changed files with 54 additions and 12 deletions

View file

@ -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<Parser>();
services.AddMediatR(typeof(Program).Assembly);
services.AddAgentHttpClient();
services.AddSingleton<IFilterListsApiClient, FilterListsApiClient>();
@ -39,15 +41,6 @@ private static void AddConfiguration(this IServiceCollection services)
services.Configure<GitHub>(config.GetSection(nameof(GitHub)));
}
private static void AddAgentHttpClient(this IServiceCollection services)
{
services.AddHttpClient<IAgentHttpClient, AgentHttpClient>().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<IAgentHttpClient, AgentHttpClient>().ConfigureHttpMessageHandlerBuilder(b =>
{
b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false};
b.Build();
});
}
}
}

View file

@ -27,6 +27,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.5.0" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="LibGit2Sharp" Version="0.26.0" />
<PackageReference Include="MediatR" Version="7.0.0" />

View file

@ -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; }
}
}

View file

@ -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<Parser>();
var mediator = _serviceProvider.GetService<IMediator>();
await mediator.Send(new CaptureLists.Command());
await mediator.Send(new ValidateAllUrls.Command());
await parser.ParseArguments<Options>(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()

View file

@ -0,0 +1,8 @@
{
"profiles": {
"FilterLists.Agent": {
"commandName": "Project",
"commandLineArgs": "-c -v"
}
}
}