Skip to content

Commit 171a39a

Browse files
authored
Merge pull request #20 from dmnyu/creation-service
creating seperate test fixutres for bag's created by bagit.py and bagit.net
2 parents 5fb8d5b + 9efc17c commit 171a39a

38 files changed

+357
-260
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Set default behavior to automatically normalize line endings.
33
###############################################################################
44
* text=auto
5-
5+
*.txt text eol=lf
66
###############################################################################
77
# Set default behavior for command prompt diff.
88
#

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: BagIt.NET CI
22

33
on:
4+
push:
5+
branches: ["*"]
46
pull_request:
57
branches: [ main ]
68

bagit.net.cli/Bagger.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using bagit.net.domain;
2+
using bagit.net.interfaces;
3+
using Microsoft.Extensions.Logging;
4+
using Spectre.Console;
5+
6+
namespace bagit.net.cli
7+
{
8+
public class Bagger
9+
{
10+
readonly ICreationService _creationService;
11+
readonly ILogger _logger;
12+
13+
public Bagger(ILogger<Bagger> logger, ICreationService creationService)
14+
{
15+
_logger = logger;
16+
_creationService = creationService;
17+
}
18+
19+
public int CreateBag(string? dirLocation, string? checkSumAlgorithm, string? logFile, CancellationToken cancellationToken)
20+
{
21+
if (string.IsNullOrWhiteSpace(dirLocation))
22+
{
23+
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
24+
AnsiConsole.MarkupLine("[red]a directory to bag must be specified when creating a bag[/]\n");
25+
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
26+
return 1;
27+
}
28+
29+
var bagPath = Path.GetFullPath(dirLocation);
30+
if (!Directory.Exists(bagPath))
31+
{
32+
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
33+
AnsiConsole.MarkupLine($"[red]the directory {bagPath} does not exist[/]\n");
34+
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
35+
return 1;
36+
}
37+
38+
//get the algorithm
39+
ChecksumAlgorithm algorithm;
40+
if (string.IsNullOrWhiteSpace(checkSumAlgorithm))
41+
{
42+
algorithm = ChecksumAlgorithm.SHA256;
43+
}
44+
else if (ChecksumAlgorithmMap.Algorithms.ContainsKey(checkSumAlgorithm))
45+
{
46+
algorithm = ChecksumAlgorithmMap.Algorithms[checkSumAlgorithm];
47+
}
48+
else
49+
{
50+
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
51+
AnsiConsole.MarkupLine($"[red]checksum algorithm {checkSumAlgorithm} is not supported[/]\n");
52+
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
53+
return 1;
54+
}
55+
56+
//get logging option
57+
if (!string.IsNullOrWhiteSpace(logFile))
58+
{
59+
AnsiConsole.MarkupLine($"bagit.net.cli v{Bagit.VERSION}");
60+
AnsiConsole.MarkupLine($"Logging to {logFile}");
61+
}
62+
63+
_creationService.CreateBag(bagPath, algorithm);
64+
return 0;
65+
}
66+
}
67+
}

bagit.net.cli/BagitCLI.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using bagit.net.cli.Commands;
2+
using bagit.net.domain;
23
using Spectre.Console;
34
using Spectre.Console.Cli;
45

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using bagit.net.interfaces;
2-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
32
using Spectre.Console;
43
using Spectre.Console.Cli;
54
using System.ComponentModel;
@@ -26,49 +25,16 @@ public class Settings : CommandSettings
2625

2726
public override int Execute(CommandContext context, Settings settings, CancellationToken cancellationToken)
2827
{
29-
30-
if (string.IsNullOrWhiteSpace(settings.Directory))
28+
try
3129
{
32-
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
33-
AnsiConsole.MarkupLine("[red]a directory to bag must be specified when creating a bag[/]\n");
34-
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
35-
return 1;
30+
var serviceProvider = ServiceConfigurator.BuildServiceProvider<Bagger>(settings.logFile);
31+
var bagger = serviceProvider.GetRequiredService<Bagger>();
32+
bagger.CreateBag(settings.Directory, settings.Algorithm, settings.logFile, cancellationToken);
3633
}
37-
38-
var bagPath = Path.GetFullPath(settings.Directory);
39-
if (!Directory.Exists(bagPath)) {
40-
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
41-
AnsiConsole.MarkupLine($"[red]the directory {bagPath} does not exist[/]\n");
42-
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
34+
catch (Exception ex) {
35+
AnsiConsole.MarkupLine($"[red][bold]ERROR:[/] {ex.Message}");
4336
return 1;
4437
}
45-
46-
//get the algorithm
47-
ChecksumAlgorithm algorithm;
48-
if (string.IsNullOrWhiteSpace(settings.Algorithm))
49-
{
50-
algorithm = ChecksumAlgorithm.SHA256;
51-
} else if(ChecksumAlgorithmMap.Algorithms.ContainsKey(settings.Algorithm))
52-
{
53-
algorithm = ChecksumAlgorithmMap.Algorithms[settings.Algorithm];
54-
} else
55-
{
56-
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
57-
AnsiConsole.MarkupLine($"[red]checksum algorithm {settings.Algorithm} is not supported[/]\n");
58-
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
59-
return 1;
60-
}
61-
62-
//get logging option
63-
if (!string.IsNullOrWhiteSpace(settings.logFile))
64-
{
65-
AnsiConsole.MarkupLine($"bagit.net.cli v{Bagit.VERSION}");
66-
AnsiConsole.MarkupLine($"Logging to {settings.logFile}");
67-
}
68-
69-
var serviceProvider = ServiceConfigurator.BuildServiceProvider<Bagger>();
70-
var bagger = serviceProvider.GetRequiredService<Bagger>();
71-
bagger.CreateBag(bagPath, algorithm);
7238
return 0;
7339
}
7440
}

bagit.net.cli/Commands/HelpCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using bagit.net.domain;
12
using Spectre.Console;
23
using Spectre.Console.Cli;
34

bagit.net.cli/Commands/ValidateCommand.cs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Extensions.DependencyInjection;
1+
using bagit.net.domain;
2+
using Microsoft.Extensions.DependencyInjection;
23
using Spectre.Console;
34
using Spectre.Console.Cli;
45
using System.ComponentModel;
@@ -22,33 +23,16 @@ public class Settings : CommandSettings
2223

2324
public override int Execute(CommandContext context, Settings settings, CancellationToken cancellationToken)
2425
{
25-
if (string.IsNullOrWhiteSpace(settings.Directory))
26+
try
2627
{
27-
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
28-
AnsiConsole.MarkupLine("[red]a directory to a BagIt bag must be specified when creating a bag[/]\n");
29-
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
30-
return 1;
28+
var serviceProvider = ServiceConfigurator.BuildServiceProvider<Validator>(settings.logFile);
29+
var validator = serviceProvider.GetRequiredService<Validator>();
30+
validator.ValidateBag(settings.Directory, settings.Fast, settings.logFile, cancellationToken);
3131
}
32-
33-
var bagPath = Path.GetFullPath(settings.Directory);
34-
if (!Directory.Exists(bagPath))
35-
{
36-
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
37-
AnsiConsole.MarkupLine($"[red]the directory {bagPath} does not exist[/]\n");
38-
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
32+
catch (Exception ex) {
33+
AnsiConsole.MarkupLine($"[red][bold]ERROR:[/] {ex.Message}");
3934
return 1;
4035
}
41-
42-
if (!string.IsNullOrWhiteSpace(settings.logFile))
43-
{
44-
AnsiConsole.MarkupLine($"bagit.net.cli v{Bagit.VERSION}");
45-
AnsiConsole.MarkupLine($"Logging to {settings.logFile}");
46-
}
47-
48-
var serviceProvider = ServiceConfigurator.BuildServiceProvider<Validator>();
49-
var validator = serviceProvider.GetRequiredService<Validator>();
50-
validator.ValidateBag(bagPath, settings.Fast);
51-
5236
return 0;
5337
}
5438
}

bagit.net/Logging.cs renamed to bagit.net.cli/Logging.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Serilog.Events;
22
using Serilog.Formatting;
33

4-
namespace bagit.net;
4+
namespace bagit.net.cli;
55

66
public class ShortLevelFormatter : ITextFormatter
77
{

bagit.net/Bagit.cs renamed to bagit.net.cli/ServiceConfigurator.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@
33
using Microsoft.Extensions.DependencyInjection;
44
using Serilog;
55

6-
namespace bagit.net
7-
{
8-
public static class Bagit
9-
{
10-
public const string VERSION = "0.2.3-alpha";
11-
public const string BAGIT_VERSION = "1.0";
12-
13-
}
146

7+
namespace bagit.net.cli
8+
{
159
public static class ServiceConfigurator
1610
{
17-
public static ServiceProvider BuildServiceProvider<TWorker>(string logFile = "")
11+
public static ServiceProvider BuildServiceProvider<TWorker>(string? logFile = "")
1812
where TWorker : class
1913
{
2014

@@ -40,8 +34,9 @@ public static ServiceProvider BuildServiceProvider<TWorker>(string logFile = "")
4034
services.AddSingleton<ITagFileService, TagFileService>();
4135
services.AddSingleton<IFileManagerService, FileManagerService>();
4236
services.AddSingleton<IValidationService, ValidationService>();
37+
services.AddSingleton<ICreationService, CreationService>();
4338
services.AddTransient<TWorker>();
4439
return services.BuildServiceProvider();
4540
}
4641
}
47-
}
42+
}

bagit.net.cli/Validator.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using bagit.net.domain;
2+
using bagit.net.interfaces;
3+
using Microsoft.Extensions.Logging;
4+
using Spectre.Console;
5+
6+
namespace bagit.net.cli
7+
{
8+
public class Validator
9+
{
10+
private readonly ILogger _logger;
11+
private readonly IValidationService _validationService;
12+
public Validator(ILogger<Validator> logger, IValidationService validationService)
13+
{
14+
_logger = logger;
15+
_validationService = validationService;
16+
}
17+
public int ValidateBag(string? bagPath, bool fast, string? logFile, CancellationToken cancellationToken)
18+
{
19+
_logger.LogInformation($"Using bagit.net v{Bagit.VERSION}");
20+
21+
try
22+
{
23+
24+
if (string.IsNullOrWhiteSpace(bagPath))
25+
{
26+
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
27+
AnsiConsole.MarkupLine("[red]a directory to a BagIt bag must be specified when creating a bag[/]\n");
28+
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
29+
return 1;
30+
}
31+
32+
bagPath = Path.GetFullPath(bagPath);
33+
if (!Directory.Exists(bagPath))
34+
{
35+
AnsiConsole.MarkupLine("[red][bold]ERROR:[/][/]");
36+
AnsiConsole.MarkupLine($"[red]the directory {bagPath} does not exist[/]\n");
37+
BagitCLI.app.Run(new string[] { "help" }, cancellationToken);
38+
return 1;
39+
}
40+
41+
if (!string.IsNullOrWhiteSpace(logFile))
42+
{
43+
AnsiConsole.MarkupLine($"bagit.net.cli v{Bagit.VERSION}");
44+
AnsiConsole.MarkupLine($"Logging to {logFile}");
45+
}
46+
47+
if (fast)
48+
{
49+
_validationService.ValidateBag(bagPath);
50+
_logger.LogInformation($"{bagPath} is valid according to Payload-Oxum");
51+
return 0;
52+
}
53+
54+
_validationService.ValidateBag(bagPath);
55+
_logger.LogInformation($"{bagPath} is valid");
56+
return 0;
57+
} catch (Exception ex) {
58+
_logger.LogCritical(ex, "Failed to validate bag at {Path}", bagPath);
59+
throw;
60+
}
61+
}
62+
63+
}
64+
}

0 commit comments

Comments
 (0)