using System.CommandLine; using System.CommandLine.Parsing; using ScadaLink.Commons.Messages.Management; namespace ScadaLink.CLI.Commands; public static class ExternalSystemCommands { public static Command Build(Option contactPointsOption, Option formatOption) { var command = new Command("external-system") { Description = "Manage external systems" }; command.Add(BuildList(contactPointsOption, formatOption)); command.Add(BuildCreate(contactPointsOption, formatOption)); command.Add(BuildDelete(contactPointsOption, formatOption)); return command; } private static Command BuildList(Option contactPointsOption, Option formatOption) { var cmd = new Command("list") { Description = "List all external systems" }; cmd.SetAction(async (ParseResult result) => { return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new ListExternalSystemsCommand()); }); return cmd; } private static Command BuildCreate(Option contactPointsOption, Option formatOption) { var nameOption = new Option("--name") { Description = "System name", Required = true }; var urlOption = new Option("--endpoint-url") { Description = "Endpoint URL", Required = true }; var authTypeOption = new Option("--auth-type") { Description = "Auth type (ApiKey, BasicAuth)", Required = true }; var authConfigOption = new Option("--auth-config") { Description = "Auth configuration JSON" }; var cmd = new Command("create") { Description = "Create an external system" }; cmd.Add(nameOption); cmd.Add(urlOption); cmd.Add(authTypeOption); cmd.Add(authConfigOption); cmd.SetAction(async (ParseResult result) => { var name = result.GetValue(nameOption)!; var url = result.GetValue(urlOption)!; var authType = result.GetValue(authTypeOption)!; var authConfig = result.GetValue(authConfigOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new CreateExternalSystemCommand(name, url, authType, authConfig)); }); return cmd; } private static Command BuildDelete(Option contactPointsOption, Option formatOption) { var idOption = new Option("--id") { Description = "External system ID", Required = true }; var cmd = new Command("delete") { Description = "Delete an external system" }; cmd.Add(idOption); cmd.SetAction(async (ParseResult result) => { var id = result.GetValue(idOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new DeleteExternalSystemCommand(id)); }); return cmd; } }