Add 'metadata-schema' command to generate JSON schema for Aaru metadata files

This commit is contained in:
2026-01-19 09:40:22 +00:00
parent f87a48aadd
commit c9404499e7
5 changed files with 162 additions and 0 deletions

View File

@@ -10859,5 +10859,35 @@ namespace Aaru.Localization {
return ResourceManager.GetString("Enables_calculation_of_Spamsum_fuzzy_hashes", resourceCulture);
}
}
public static string Output_is_required {
get {
return ResourceManager.GetString("Output_is_required", resourceCulture);
}
}
public static string Generating_JSON_schema_for_metadata {
get {
return ResourceManager.GetString("Generating_JSON_schema_for_metadata", resourceCulture);
}
}
public static string Schema_successfully_written_to_0 {
get {
return ResourceManager.GetString("Schema_successfully_written_to_0", resourceCulture);
}
}
public static string Output_file_for_the_JSON_schema {
get {
return ResourceManager.GetString("Output_file_for_the_JSON_schema", resourceCulture);
}
}
public static string Generates_the_JSON_schema_for_Aaru_metadata_files {
get {
return ResourceManager.GetString("Generates_the_JSON_schema_for_Aaru_metadata_files", resourceCulture);
}
}
}
}

View File

@@ -5425,4 +5425,19 @@ Probadores:
<data name="Enables_calculation_of_Spamsum_fuzzy_hashes" xml:space="preserve">
<value>Activa calcular los hashes Spamsum.</value>
</data>
<data name="Output_is_required" xml:space="preserve">
<value>[red]Se requiere una salida[/]</value>
</data>
<data name="Generating_JSON_schema_for_metadata" xml:space="preserve">
<value>[slateblue1]Generadon esquema JSON para los metadatados…[/]</value>
</data>
<data name="Schema_successfully_written_to_0" xml:space="preserve">
<value>[slateblue1]Esquema escrito con éxito en {0}[/]</value>
</data>
<data name="Output_file_for_the_JSON_schema" xml:space="preserve">
<value>Fichero de salida para el esquema JSON</value>
</data>
<data name="Generates_the_JSON_schema_for_Aaru_metadata_files" xml:space="preserve">
<value>Genera el esquema JSON para los ficheros de metadatos Aaru</value>
</data>
</root>

View File

@@ -5509,4 +5509,19 @@ Do you want to continue?</value>
<data name="Enables_calculation_of_Spamsum_fuzzy_hashes" xml:space="preserve">
<value>Enables calculation of Spamsum fuzzy hashes.</value>
</data>
<data name="Output_is_required" xml:space="preserve">
<value>[red]Output is required[/]</value>
</data>
<data name="Generating_JSON_schema_for_metadata" xml:space="preserve">
<value>[slateblue1]Generating JSON schema for metadata…[/]</value>
</data>
<data name="Schema_successfully_written_to_0" xml:space="preserve">
<value>[slateblue1]Schema successfully written to {0}[/]</value>
</data>
<data name="Output_file_for_the_JSON_schema" xml:space="preserve">
<value>Output file for the JSON schema</value>
</data>
<data name="Generates_the_JSON_schema_for_Aaru_metadata_files" xml:space="preserve">
<value>Generates the JSON schema for Aaru metadata files</value>
</data>
</root>

View File

@@ -0,0 +1,99 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : MetadataSchema.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Commands.
//
// --[ Description ] ----------------------------------------------------------
//
// Implements the 'metadata-schema' command.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2026 Natalia Portillo
// ****************************************************************************/
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Schema;
using System.Threading;
using Aaru.CommonTypes;
using Aaru.CommonTypes.AaruMetadata;
using Aaru.CommonTypes.Enums;
using Aaru.Core;
using Aaru.Localization;
using Aaru.Logging;
using Spectre.Console.Cli;
using IOFile = System.IO.File;
namespace Aaru.Commands;
sealed class MetadataSchemaCommand : Command<MetadataSchemaCommand.Settings>
{
const string MODULE_NAME = "Metadata Schema command";
public override int Execute(CommandContext context, Settings settings, CancellationToken cancellationToken)
{
MainClass.PrintCopyright();
Statistics.AddCommand("metadata-schema");
AaruLogging.Debug(MODULE_NAME, "--debug={0}", settings.Debug);
AaruLogging.Debug(MODULE_NAME, "--verbose={0}", settings.Verbose);
AaruLogging.Debug(MODULE_NAME, "--output={0}", settings.Output);
if(string.IsNullOrWhiteSpace(settings.Output))
{
AaruLogging.Error(UI.Output_is_required);
return (int)ErrorNumber.InvalidArgument;
}
AaruLogging.WriteLine(UI.Generating_JSON_schema_for_metadata);
// Create options with the MetadataJsonContext as TypeInfoResolver
var options = new JsonSerializerOptions
{
WriteIndented = true,
TypeInfoResolver = new MetadataJsonContext()
};
JsonNode schema = options.GetJsonSchemaAsNode(typeof(MetadataJson));
string jsonString = schema.ToJsonString(options);
IOFile.WriteAllText(settings.Output, jsonString);
AaruLogging.WriteLine(UI.Schema_successfully_written_to_0, settings.Output);
return (int)ErrorNumber.NoError;
}
#region Nested type: Settings
public class Settings : BaseSettings
{
[CommandArgument(0, "<output>")]
[LocalizedDescription(nameof(UI.Output_file_for_the_JSON_schema))]
public string Output { get; set; }
}
#endregion
}

View File

@@ -460,6 +460,9 @@ class MainClass
.WithAlias("rem")
.WithDescription(UI.Remote_Command_Description);
config.AddCommand<MetadataSchemaCommand>("metadata-schema")
.WithDescription(UI.Generates_the_JSON_schema_for_Aaru_metadata_files);
config.SetInterceptor(new LoggingInterceptor());
config.SetInterceptor(new PausingInterceptor());
});