2026-10-10 19:58:04 +02:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace Bifrost.Web.Features.ConceptVotes;
|
|
|
|
|
|
|
|
|
|
// Host command: Bifrost.Web.dll concepts plan|apply <collection> <manifest.json> [--retire k1,k2]
|
|
|
|
|
public static class ConceptCommand
|
|
|
|
|
{
|
|
|
|
|
public const int Ok = 0;
|
|
|
|
|
public const int Refused = 1;
|
|
|
|
|
public const int Usage = 2;
|
|
|
|
|
|
|
|
|
|
// args are the words after "concepts".
|
|
|
|
|
public static async Task<int> RunAsync(string[] args, IServiceProvider services, TextWriter output)
|
|
|
|
|
{
|
|
|
|
|
var known = args.Length > 0 && args[0] is "plan" or "apply";
|
|
|
|
|
var hasRetire = args.Length == 5 && args[3] == "--retire";
|
|
|
|
|
if (!known || !(args.Length == 3 || hasRetire))
|
|
|
|
|
{
|
|
|
|
|
await output.WriteLineAsync("usage: concepts plan|apply <collection> <manifest.json> [--retire k1,k2]");
|
|
|
|
|
return Usage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] retire = hasRetire ? args[4].Split(',', StringSplitOptions.RemoveEmptyEntries) : [];
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-10-10 20:34:03 +02:00
|
|
|
var ships = ConceptSubjectRegistry.ParseManifest(await ReadManifestAsync(args[2]));
|
2026-10-10 19:58:04 +02:00
|
|
|
await using var scope = services.CreateAsyncScope();
|
|
|
|
|
var registry = scope.ServiceProvider.GetRequiredService<ConceptSubjectRegistry>();
|
|
|
|
|
var plan = args[0] == "apply"
|
|
|
|
|
? await registry.ApplyAsync(args[1], ships, retire, default)
|
|
|
|
|
: await registry.PlanAsync(args[1], ships, retire, default);
|
|
|
|
|
|
|
|
|
|
foreach (var entry in plan.Entries)
|
|
|
|
|
{
|
|
|
|
|
await output.WriteLineAsync($"{entry.Change.ToString().ToLowerInvariant()} {entry.Key}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok;
|
|
|
|
|
}
|
|
|
|
|
catch (ManifestRefusedException ex)
|
|
|
|
|
{
|
|
|
|
|
await output.WriteLineAsync($"refused {ex.Field} {ex.Code}");
|
|
|
|
|
return Refused;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-10 20:34:03 +02:00
|
|
|
|
2026-10-10 23:01:40 +02:00
|
|
|
// Reads at most one byte past the bound, so a wrong file is never read whole.
|
|
|
|
|
private static async Task<byte[]> ReadManifestAsync(string path)
|
2026-10-10 20:34:03 +02:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-10-10 23:01:40 +02:00
|
|
|
await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1, true);
|
|
|
|
|
var buffer = new byte[ConceptSubjectRegistry.MaxManifestBytes + 1];
|
|
|
|
|
var length = 0;
|
|
|
|
|
int read;
|
|
|
|
|
while (length < buffer.Length && (read = await stream.ReadAsync(buffer.AsMemory(length))) > 0)
|
|
|
|
|
{
|
|
|
|
|
length += read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buffer[..length];
|
2026-10-10 20:34:03 +02:00
|
|
|
}
|
2026-10-10 23:01:40 +02:00
|
|
|
// An empty or NUL path is an ArgumentException; a path the platform cannot name is NotSupported.
|
|
|
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException
|
|
|
|
|
or NotSupportedException or System.Security.SecurityException)
|
2026-10-10 20:34:03 +02:00
|
|
|
{
|
|
|
|
|
throw new ManifestRefusedException("manifest", "Unreadable");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-10 19:58:04 +02:00
|
|
|
}
|