using CliFx;
using CliFx.Attributes;
using CliFx.Infrastructure;
namespace OpcUaCli.Commands;
[Command("connect", Description = "Test connection to an OPC UA server")]
public class ConnectCommand : ICommand
{
///
/// Gets the OPC UA endpoint URL to test.
///
[CommandOption("url", 'u', Description = "OPC UA server endpoint URL", IsRequired = true)]
public string Url { get; init; } = default!;
[CommandOption("username", 'U', Description = "Username for authentication")]
public string? Username { get; init; }
[CommandOption("password", 'P', Description = "Password for authentication")]
public string? Password { get; init; }
///
/// Connects to the OPC UA endpoint and prints the resolved server metadata.
///
/// The console used to report connection results.
public async ValueTask ExecuteAsync(IConsole console)
{
using var session = await OpcUaHelper.ConnectAsync(Url, Username, Password);
await console.Output.WriteLineAsync($"Connected to: {session.Endpoint.EndpointUrl}");
await console.Output.WriteLineAsync($"Server: {session.Endpoint.Server!.ApplicationName}");
await console.Output.WriteLineAsync($"Security Mode: {session.Endpoint.SecurityMode}");
await console.Output.WriteLineAsync($"Security Policy: {session.Endpoint.SecurityPolicyUri}");
await console.Output.WriteLineAsync("Connection successful.");
}
}