using System.Text.Json;
using System.Text.Json.Nodes;
using Npgsql;
using NpgsqlRest;
using NpgsqlRest.HttpFiles;
using NpgsqlRest.TsClient;
namespace NpgsqlRestClient;
///
/// Provides machine-readable JSON output for CLI commands.
/// Used by pgdev and other tools to programmatically inspect NpgsqlRest.
///
public static class CliJson
{
public static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true };
private static string GetVersion() =>
System.Reflection.Assembly.GetAssembly(typeof(T))?.GetName().Version?.ToString() ?? "-";
public static JsonObject GetVersionJson()
{
var frameworkParts = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.Split(' ', 2);
return new JsonObject
{
["runtime"] = new JsonObject
{
["name"] = frameworkParts.Length > 0 ? frameworkParts[0] : "-",
["version"] = frameworkParts.Length > 1 ? frameworkParts[1] : "-"
},
["platform"] = System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier,
["versions"] = new JsonObject
{
["NpgsqlRest"] = GetVersion(),
["NpgsqlRestClient"] = GetVersion(),
["NpgsqlRest.HttpFiles"] = GetVersion(),
["NpgsqlRest.TsClient"] = GetVersion(),
["NpgsqlRest.SqlFileSource"] = GetVersion(),
["NpgsqlRest.OpenApi"] = GetVersion(),
["Npgsql"] = GetVersion(),
["ExcelDataReader"] = GetVersion(),
["SpreadCheetah"] = GetVersion(),
["Serilog.AspNetCore"] = GetVersion(),
["Serilog.Sinks.OpenTelemetry"] = GetVersion(),
["System.Text.Json"] = GetVersion(),
["StackExchange.Redis"] = GetVersion(),
["Microsoft.Extensions.Caching.Hybrid"] = GetVersion(),
["Microsoft.Extensions.Caching.StackExchangeRedis"] = GetVersion(),
["Microsoft.AspNetCore.Authentication.JwtBearer"] = GetVersion(),
["AspNetCore.HealthChecks.NpgSql"] = GetVersion()
},
["currentDirectory"] = Directory.GetCurrentDirectory(),
["baseDirectory"] = AppContext.BaseDirectory
};
}
}