Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate catlet and fodder genes during packing #8

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Eryph.GenePool.Model/Eryph.GenePool.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Dbosoft.Functional" Version="3.1.0" />
<PackageReference Include="Eryph.ConfigModel.Core.Validation" Version="0.3.1-ci.2" />
<PackageReference Include="Eryph.ConfigModel.Core.Validation" Version="0.3.1-ci.3" />
<PackageReference Include="IsExternalInit" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
6 changes: 3 additions & 3 deletions src/Eryph.GenePool.Packing/Eryph.GenePool.Packing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Eryph.ConfigModel.Catlets.Validation" Version="0.3.1-ci.2" />
<PackageReference Include="Eryph.ConfigModel.Catlets.Yaml" Version="0.3.1-ci.2" />
<PackageReference Include="Eryph.ConfigModel.System.Json" Version="0.3.1-ci.2" />
<PackageReference Include="Eryph.ConfigModel.Catlets.Validation" Version="0.3.1-ci.3" />
<PackageReference Include="Eryph.ConfigModel.Catlets.Yaml" Version="0.3.1-ci.3" />
<PackageReference Include="Eryph.ConfigModel.System.Json" Version="0.3.1-ci.3" />
<PackageReference Include="Joveler.Compression.XZ" Version="4.3.0" />
</ItemGroup>

Expand Down
48 changes: 44 additions & 4 deletions src/apps/src/eryph-packer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.CommandLine.Parsing;
using System.CommandLine.Rendering;
using System.Text.Json;
using Eryph.ConfigModel;
using Eryph.ConfigModel.Catlets;
using Eryph.ConfigModel.FodderGenes;
using Eryph.ConfigModel.Json;
Expand All @@ -12,8 +13,11 @@
using Eryph.GenePool.Model;
using Eryph.GenePool.Packing;
using Eryph.Packer;
using LanguageExt;
using LanguageExt.Common;
using Spectre.Console;
using Spectre.Console.Json;
using Spectre.Console.Rendering;
using Command = System.CommandLine.Command;

//AnsiConsole.Profile.Capabilities.Interactive = false;
Expand All @@ -25,17 +29,17 @@
var vmExportArgument = new Argument<DirectoryInfo>("vm export", "path to exported VM");
var filePathArgument = new Argument<FileInfo>("file", "path to file");

var isPublicOption = new Option<bool>("--public", "sets genesets visibility to public");
var shortDescriptionOption = new Option<string>("--description", "sets genesets description");
var isPublicOption = new System.CommandLine.Option<bool>("--public", "sets genesets visibility to public");
var shortDescriptionOption = new System.CommandLine.Option<string>("--description", "sets genesets description");

vmExportArgument.ExistingOnly();

var apiKeyOption =
new Option<string>("--api-key", "API key for authentication");
new System.CommandLine.Option<string>("--api-key", "API key for authentication");

var workDirOption =
// ReSharper disable once StringLiteralTypo
new Option<DirectoryInfo>("--workdir", "work directory")
new System.CommandLine.Option<DirectoryInfo>("--workdir", "work directory")
.ExistingOnly();

workDirOption.SetDefaultValue(new DirectoryInfo(Environment.CurrentDirectory));
Expand Down Expand Up @@ -255,6 +259,11 @@
{
var catletContent = File.ReadAllText(catletFile);
var catletConfig = DeserializeCatletConfigString(catletContent);
var validationResult = CatletConfigValidations.ValidateCatletConfig(catletConfig);
validationResult.ToEither()
.MapLeft(issues => Error.New("The catlet configuration is invalid.",
Error.Many(issues.Map(i => i.ToError()))))
.IfLeft(e => e.Throw());
var configJson = ConfigModelJsonSerializer.Serialize(catletConfig);
await File.WriteAllTextAsync(Path.Combine(packFolder, "catlet.json"), configJson);
packableFiles.Add(new PackableFile(Path.Combine(packFolder, "catlet.json"),
Expand All @@ -278,6 +287,11 @@
{
var fodderContent = File.ReadAllText(fodderFile.FullName);
var fodderConfig = DeserializeFodderConfigString(fodderContent);
var validationResult = FodderGeneConfigValidations.ValidateFodderGeneConfig(fodderConfig);
validationResult.ToEither()
.MapLeft(issues => Error.New($"The fodder configuration '{fodderFile.Name}' is invalid.",
Error.Many(issues.Map(i => i.ToError()))))
.IfLeft(e => e.Throw());
var fodderJson = ConfigModelJsonSerializer.Serialize(fodderConfig);
var fodderPackFolder = Path.Combine(packFolder, "fodder");
if (!Directory.Exists(fodderPackFolder))
Expand Down Expand Up @@ -606,6 +620,32 @@ await AnsiConsole.Status()
{
AnsiConsole.MarkupLineInterpolated($"[red]{ex.Message}[/]");
}
else if (ex is ErrorException eex)
{
var error = eex.ToError();

Grid createGrid() => new Grid()
.AddColumn(new GridColumn() { Width = 2 })
.AddColumn();

Grid addRow(Grid grid, IRenderable renderable) =>
grid.AddRow(new Markup(""), renderable);

Grid addToGrid(Grid grid, Error error) => error switch
{
ManyErrors me => me.Errors.Fold(grid, addToGrid),
Exceptional ee => addRow(grid, ee.ToException().GetRenderable()),
_ => addRow(grid, new Text(error.Message))
.Apply(g => error.Inner.Match(
Some: ie => addRow(g, addToGrid(createGrid(), ie)),
None: () => g)),
};

AnsiConsole.Write(new Rows(
new Markup("[red]The operation failed with following error(s):[/]"),
addToGrid(createGrid(), error)));
AnsiConsole.WriteLine();
}
else
{
AnsiConsole.MarkupLine("[red]The operation failed with the following error:[/]");
Expand Down