using System.CommandLine;
using System.CommandLine.Parsing;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
public static class SiteCommands
{
///
/// Builds the site command group and all its subcommands.
///
/// Global management URL option.
/// Global output format option.
/// Global username option.
/// Global password option.
/// The configured site command with all subcommands attached.
public static Command Build(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption)
{
var command = new Command("site") { Description = "Manage sites" };
command.Add(BuildList(urlOption, formatOption, usernameOption, passwordOption));
command.Add(BuildGet(urlOption, formatOption, usernameOption, passwordOption));
command.Add(BuildCreate(urlOption, formatOption, usernameOption, passwordOption));
command.Add(BuildUpdate(urlOption, formatOption, usernameOption, passwordOption));
command.Add(BuildDelete(urlOption, formatOption, usernameOption, passwordOption));
command.Add(BuildDeployArtifacts(urlOption, formatOption, usernameOption, passwordOption));
command.Add(BuildArea(urlOption, formatOption, usernameOption, passwordOption));
return command;
}
private static Command BuildGet(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption)
{
var idOption = new Option("--id") { Description = "Site ID", Required = true };
var cmd = new Command("get") { Description = "Get a site by ID" };
cmd.Add(idOption);
cmd.SetAction(async (ParseResult result) =>
{
var id = result.GetValue(idOption);
return await CommandHelpers.ExecuteCommandAsync(
result, urlOption, formatOption, usernameOption, passwordOption, new GetSiteCommand(id));
});
return cmd;
}
private static Command BuildList(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption)
{
var cmd = new Command("list") { Description = "List all sites" };
cmd.SetAction(async (ParseResult result) =>
{
return await CommandHelpers.ExecuteCommandAsync(
result, urlOption, formatOption, usernameOption, passwordOption, new ListSitesCommand());
});
return cmd;
}
private static Command BuildCreate(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption)
{
var nameOption = new Option("--name") { Description = "Site name", Required = true };
var identifierOption = new Option("--identifier") { Description = "Site identifier", Required = true };
var descOption = new Option("--description") { Description = "Site description" };
var nodeAOption = new Option("--node-a-address") { Description = "Akka address for Node A" };
var nodeBOption = new Option("--node-b-address") { Description = "Akka address for Node B" };
var grpcNodeAOption = new Option("--grpc-node-a-address") { Description = "gRPC address for Node A" };
var grpcNodeBOption = new Option("--grpc-node-b-address") { Description = "gRPC address for Node B" };
var cmd = new Command("create") { Description = "Create a new site" };
cmd.Add(nameOption);
cmd.Add(identifierOption);
cmd.Add(descOption);
cmd.Add(nodeAOption);
cmd.Add(nodeBOption);
cmd.Add(grpcNodeAOption);
cmd.Add(grpcNodeBOption);
cmd.SetAction(async (ParseResult result) =>
{
var name = result.GetValue(nameOption)!;
var identifier = result.GetValue(identifierOption)!;
var desc = result.GetValue(descOption);
var nodeA = result.GetValue(nodeAOption);
var nodeB = result.GetValue(nodeBOption);
var grpcNodeA = result.GetValue(grpcNodeAOption);
var grpcNodeB = result.GetValue(grpcNodeBOption);
return await CommandHelpers.ExecuteCommandAsync(
result, urlOption, formatOption, usernameOption, passwordOption,
new CreateSiteCommand(name, identifier, desc, nodeA, nodeB, grpcNodeA, grpcNodeB));
});
return cmd;
}
///
/// Builds the partial update subcommand (CLI-2): only the options actually
/// passed are sent; an omitted option travels as null and the server keeps
/// the stored value. is a test seam that
/// receives the built command record instead of sending it.
///
internal static Command BuildUpdate(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption,
Func