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(BuildCreate(contactPointsOption, formatOption)); command.Add(BuildDelete(contactPointsOption, formatOption)); command.Add(BuildDeployArtifacts(contactPointsOption, formatOption)); return command; } 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 cmd = new Command("create") { Description = "Create a new site" }; cmd.Add(nameOption); cmd.Add(identifierOption); cmd.Add(descOption); cmd.SetAction(async (ParseResult result) => { var name = result.GetValue(nameOption)!; var identifier = result.GetValue(identifierOption)!; var desc = result.GetValue(descOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new CreateSiteCommand(name, identifier, desc)); }); 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 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; } }