feat(archival): 🚧 WIP impl UoW w/git

This commit is contained in:
Collin M. Barrett 2020-09-12 19:35:20 -05:00
parent dc8d1a3426
commit 9111ab80db
5 changed files with 42 additions and 22 deletions

View file

@ -20,7 +20,7 @@ public static void AddPersistenceServices(this IServiceCollection services, ICon
return new Repository(gitOptions.RepositoryDirectory);
});
services.AddTransient<IArchiveFile, GitFileArchiver>();
services.AddTransient<IArchiveFiles, GitFileArchiver>();
}
}
}

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
@ -8,8 +9,9 @@
namespace FilterLists.Archival.Infrastructure.Persistence
{
internal class GitFileArchiver : IArchiveFile
internal class GitFileArchiver : IArchiveFiles
{
private readonly IList<string> _filePaths = new List<string>();
private readonly GitOptions _options;
private readonly IRepository _repository;
@ -21,22 +23,28 @@ public GitFileArchiver(IOptions<GitOptions> gitOptions, IRepository gitRepositor
public async Task ArchiveFileAsync(
Stream fileContents,
string fileName,
string filePath,
CancellationToken cancellationToken = default)
{
await using var output = File.OpenWrite(fileName);
await using var output = File.OpenWrite(filePath);
await fileContents.CopyToAsync(output, cancellationToken);
if (!cancellationToken.IsCancellationRequested)
Commit(fileName);
_filePaths.Add(filePath);
}
private void Commit(string fileName)
public void Commit()
{
Commands.Stage(_repository, fileName);
var utcNow = DateTime.UtcNow;
var signature = new Signature(_options.UserName, _options.UserEmail, utcNow);
var message = $"archive {fileName}";
Commands.Stage(_repository, _filePaths);
var signature = new Signature(_options.UserName, _options.UserEmail, DateTime.UtcNow);
var message = $"feat(archives): archive {_filePaths.Count} files{Environment.NewLine}{string.Join(Environment.NewLine, _filePaths)}";
_repository.Commit(message, signature, signature);
}
public void Dispose()
{
foreach (var file in _filePaths)
if (File.Exists(file))
File.Delete(file);
_repository.CheckoutPaths("HEAD", _filePaths);
}
}
}

View file

@ -1,11 +0,0 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace FilterLists.Archival.Infrastructure.Persistence
{
public interface IArchiveFile
{
Task ArchiveFileAsync(Stream fileContents, string fileName, CancellationToken cancellationToken = default);
}
}

View file

@ -0,0 +1,14 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace FilterLists.Archival.Infrastructure.Persistence
{
public interface IArchiveFiles : IUnitOfWork
{
Task ArchiveFileAsync(
Stream fileContents,
string filePath,
CancellationToken cancellationToken = default);
}
}

View file

@ -0,0 +1,9 @@
using System;
namespace FilterLists.Archival.Infrastructure.Persistence
{
public interface IUnitOfWork : IDisposable
{
void Commit();
}
}