using System.CommandLine; using System.CommandLine.Parsing; using ScadaLink.Commons.Messages.Management; namespace ScadaLink.CLI.Commands; public static class SiteCommands { public static Command Build(Option contactPointsOption, Option formatOption) { var command = new Command("site") { Description = "Manage sites" }; command.Add(BuildList(contactPointsOption, formatOption)); command.Add(BuildGet(contactPointsOption, formatOption)); command.Add(BuildCreate(contactPointsOption, formatOption)); command.Add(BuildUpdate(contactPointsOption, formatOption)); command.Add(BuildDelete(contactPointsOption, formatOption)); command.Add(BuildDeployArtifacts(contactPointsOption, formatOption)); command.Add(BuildArea(contactPointsOption, formatOption)); return command; } private static Command BuildGet(Option contactPointsOption, Option formatOption) { 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, contactPointsOption, formatOption, new GetSiteCommand(id)); }); return cmd; } private static Command BuildList(Option contactPointsOption, Option formatOption) { var cmd = new Command("list") { Description = "List all sites" }; cmd.SetAction(async (ParseResult result) => { return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new ListSitesCommand()); }); return cmd; } private static Command BuildCreate(Option contactPointsOption, Option formatOption) { 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 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.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); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new CreateSiteCommand(name, identifier, desc, nodeA, nodeB)); }); return cmd; } private static Command BuildUpdate(Option contactPointsOption, Option formatOption) { var idOption = new Option("--id") { Description = "Site ID", Required = true }; var nameOption = new Option("--name") { Description = "Site name", 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 cmd = new Command("update") { Description = "Update an existing site" }; cmd.Add(idOption); cmd.Add(nameOption); cmd.Add(descOption); cmd.Add(nodeAOption); cmd.Add(nodeBOption); cmd.SetAction(async (ParseResult result) => { var id = result.GetValue(idOption); var name = result.GetValue(nameOption)!; var desc = result.GetValue(descOption); var nodeA = result.GetValue(nodeAOption); var nodeB = result.GetValue(nodeBOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new UpdateSiteCommand(id, name, desc, nodeA, nodeB)); }); return cmd; } private static Command BuildDelete(Option contactPointsOption, Option formatOption) { var idOption = new Option("--id") { Description = "Site ID", Required = true }; var cmd = new Command("delete") { Description = "Delete a site" }; cmd.Add(idOption); cmd.SetAction(async (ParseResult result) => { var id = result.GetValue(idOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new DeleteSiteCommand(id)); }); return cmd; } private static Command BuildArea(Option contactPointsOption, Option formatOption) { var group = new Command("area") { Description = "Manage areas" }; var siteIdOption = new Option("--site-id") { Description = "Site ID", Required = true }; var listCmd = new Command("list") { Description = "List areas for a site" }; listCmd.Add(siteIdOption); listCmd.SetAction(async (ParseResult result) => { var siteId = result.GetValue(siteIdOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new ListAreasCommand(siteId)); }); group.Add(listCmd); var createSiteIdOption = new Option("--site-id") { Description = "Site ID", Required = true }; var nameOption = new Option("--name") { Description = "Area name", Required = true }; var parentOption = new Option("--parent-id") { Description = "Parent area ID" }; var createCmd = new Command("create") { Description = "Create an area" }; createCmd.Add(createSiteIdOption); createCmd.Add(nameOption); createCmd.Add(parentOption); createCmd.SetAction(async (ParseResult result) => { var siteId = result.GetValue(createSiteIdOption); var name = result.GetValue(nameOption)!; var parentId = result.GetValue(parentOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new CreateAreaCommand(siteId, name, parentId)); }); group.Add(createCmd); var updateIdOption = new Option("--id") { Description = "Area ID", Required = true }; var updateNameOption = new Option("--name") { Description = "New area name", Required = true }; var updateCmd = new Command("update") { Description = "Update an area" }; updateCmd.Add(updateIdOption); updateCmd.Add(updateNameOption); updateCmd.SetAction(async (ParseResult result) => { var id = result.GetValue(updateIdOption); var name = result.GetValue(updateNameOption)!; return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new UpdateAreaCommand(id, name)); }); group.Add(updateCmd); var deleteIdOption = new Option("--id") { Description = "Area ID", Required = true }; var deleteCmd = new Command("delete") { Description = "Delete an area" }; deleteCmd.Add(deleteIdOption); deleteCmd.SetAction(async (ParseResult result) => { var id = result.GetValue(deleteIdOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new DeleteAreaCommand(id)); }); group.Add(deleteCmd); return group; } private static Command BuildDeployArtifacts(Option contactPointsOption, Option formatOption) { var siteIdOption = new Option("--site-id") { Description = "Target site ID (all sites if omitted)" }; var cmd = new Command("deploy-artifacts") { Description = "Deploy artifacts to site(s)" }; cmd.Add(siteIdOption); cmd.SetAction(async (ParseResult result) => { var siteId = result.GetValue(siteIdOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new MgmtDeployArtifactsCommand(siteId)); }); return cmd; } }