// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using AzureMcp.Core.Commands;
using AzureMcp.Core.Services.Telemetry;
using AzureMcp.Postgres.Options;
using AzureMcp.Postgres.Options.Server;
using AzureMcp.Postgres.Services;
using Microsoft.Extensions.Logging;
namespace AzureMcp.Postgres.Commands.Server;
public sealed class ServerParamGetCommand(ILogger<ServerParamGetCommand> logger) : BaseServerCommand<ServerParamGetOptions>(logger)
{
private const string CommandTitle = "Get PostgreSQL Server Parameter";
private readonly Option<string> _paramOption = PostgresOptionDefinitions.Param;
public override string Name => "param";
public override string Description =>
"Retrieves a specific parameter of a PostgreSQL server.";
public override string Title => CommandTitle;
public override ToolMetadata Metadata => new() { Destructive = false, ReadOnly = true };
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
command.AddOption(_paramOption);
}
protected override ServerParamGetOptions BindOptions(ParseResult parseResult)
{
var options = base.BindOptions(parseResult);
options.Param = parseResult.GetValueForOption(_paramOption);
return options;
}
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult)
{
try
{
var options = BindOptions(parseResult);
if (!Validate(parseResult.CommandResult, context.Response).IsValid)
{
return context.Response;
}
IPostgresService pgService = context.GetService<IPostgresService>() ?? throw new InvalidOperationException("PostgreSQL service is not available.");
var parameterValue = await pgService.GetServerParameterAsync(options.Subscription!, options.ResourceGroup!, options.User!, options.Server!, options.Param!);
context.Response.Results = parameterValue?.Length > 0 ?
ResponseResult.Create(
new ServerParamGetCommandResult(parameterValue),
PostgresJsonContext.Default.ServerParamGetCommandResult) :
null;
}
catch (Exception ex)
{
_logger.LogError(ex, "An exception occurred retrieving the parameter.");
HandleException(context, ex);
}
return context.Response;
}
internal record ServerParamGetCommandResult(string ParameterValue);
}