resolve archives git repo from DI

This commit is contained in:
Collin M. Barrett 2019-07-04 21:47:07 -05:00
parent 29b5283bc2
commit e7377e6e42
3 changed files with 27 additions and 18 deletions

View file

@ -5,6 +5,7 @@
using FilterLists.Agent.Core.Interfaces;
using FilterLists.Agent.Infrastructure.Clients;
using FilterLists.Agent.Infrastructure.Repositories;
using LibGit2Sharp;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -24,6 +25,7 @@ public static void RegisterAgentServices(this IServiceCollection services)
services.AddAgentHttpClient();
services.AddSingleton<IFilterListsApiClient, FilterListsApiClient>();
services.AddSingleton<IAgentGitHubClient, AgentGitHubClient>();
services.AddArchiveRepository();
services.AddTransient<IListInfoRepository, ListInfoRepository>();
services.AddTransient<IUrlRepository, UrlRepository>();
}
@ -59,9 +61,9 @@ private static void AddLoggingCustom(this IServiceCollection services)
services.AddLogging(b =>
{
b.AddConsole();
var appInsightsConfig = b.Services.BuildServiceProvider()
.GetService<IOptions<ApplicationInsightsSettings>>();
b.AddApplicationInsights(appInsightsConfig.Value.InstrumentationKey);
var applicationInsightsSettings = b.Services.BuildServiceProvider()
.GetService<IOptions<ApplicationInsightsSettings>>().Value;
b.AddApplicationInsights(applicationInsightsSettings.InstrumentationKey);
});
}
@ -73,5 +75,16 @@ private static void AddAgentHttpClient(this IServiceCollection services)
b.Build();
});
}
private static void AddArchiveRepository(this IServiceCollection services)
{
services.AddTransient<IRepository, Repository>(s =>
{
var archiveSettings = s.GetService<IOptions<ArchiveSettings>>().Value;
if (!Repository.IsValid(archiveSettings.RepositoryDirectory))
Repository.Init(archiveSettings.RepositoryDirectory);
return new Repository(archiveSettings.RepositoryDirectory);
});
}
}
}

View file

@ -15,25 +15,21 @@ public class Command : IRequest
public class Handler : RequestHandler<Command>
{
private readonly ArchiveSettings _archiveSettings;
private readonly IRepository _repository;
public Handler(IOptions<ArchiveSettings> archiveSettings)
public Handler(IOptions<ArchiveSettings> archiveOptions, IRepository repository)
{
_archiveSettings = archiveSettings.Value;
_archiveSettings = archiveOptions.Value;
_repository = repository;
}
protected override void Handle(Command request)
{
if (!Repository.IsValid(_archiveSettings.RepositoryDirectory))
Repository.Init(_archiveSettings.RepositoryDirectory);
using (var repo = new Repository(_archiveSettings.RepositoryDirectory))
{
Commands.Stage(repo, "*");
var utcNow = DateTime.UtcNow;
var signature = new Signature(_archiveSettings.SignatureName, _archiveSettings.SignatureEmail,
utcNow);
repo.Commit(utcNow.ToShortDateString() + _archiveSettings.CommitMessageSuffix, signature,
signature);
}
Commands.Stage(_repository, "*");
var utcNow = DateTime.UtcNow;
var signature = new Signature(_archiveSettings.SignatureName, _archiveSettings.SignatureEmail, utcNow);
_repository.Commit(utcNow.ToShortDateString() + _archiveSettings.CommitMessageSuffix, signature,
signature);
}
}
}

View file

@ -23,9 +23,9 @@ public class AgentGitHubClient : IAgentGitHubClient
private readonly GitHubSettings _gitHubSettings;
private readonly ILogger<AgentGitHubClient> _logger;
public AgentGitHubClient(IOptions<GitHubSettings> gitHubSettings, ILogger<AgentGitHubClient> logger)
public AgentGitHubClient(IOptions<GitHubSettings> gitHubOptions, ILogger<AgentGitHubClient> logger)
{
_gitHubSettings = gitHubSettings.Value;
_gitHubSettings = gitHubOptions.Value;
_logger = logger;
_gitHubClient = new GitHubClient(new ProductHeaderValue(_gitHubSettings.ProductHeaderValue))
{