using System.CommandLine; using System.CommandLine.Parsing; using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management; namespace ZB.MOM.WW.ScadaBridge.CLI.Commands; /// /// The scadabridge audit-config command group: views the configuration-change /// audit log (the IAuditService trail of admin edits — distinct from the /// centralized append-only Audit Log served by ). /// /// /// Renamed from audit-log in #23 M8-T7 to avoid confusion with the new /// scadabridge audit group. The old audit-log name is retained as a /// deprecated alias; still resolves the full subcommand /// tree, and Program.cs prints a deprecation warning when it is used. /// public static class AuditLogCommands { /// The deprecated alias kept for backward compatibility with the old command name. public const string DeprecatedAlias = "audit-log"; /// The deprecation warning emitted when the old audit-log name is used. public const string DeprecationWarning = "Warning: 'audit-log' is deprecated and will be removed in a future release. " + "Use 'audit-config' instead."; /// /// Writes the to when the /// CLI was invoked via the deprecated audit-log command name (i.e. the first /// argument is ). The command itself still works — it is /// an alias of audit-config — so this only adds the migration warning. /// Factored out of Program.cs so it is unit-testable without spawning a process. /// /// The raw command-line arguments passed to the CLI. /// The text writer to emit the deprecation warning to. public static void WriteDeprecationWarningIfNeeded(string[] args, TextWriter stderr) { if (args.Length > 0 && string.Equals(args[0], DeprecatedAlias, StringComparison.Ordinal)) { stderr.WriteLine(DeprecationWarning); } } /// /// Builds the audit-config command (with the deprecated audit-log alias) and its subcommands. /// /// Global management URL option. /// Global output format option. /// Global username option. /// Global password option. /// The configured audit-config command with all sub-commands registered. public static Command Build(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption) { var command = new Command("audit-config") { Description = "Query the configuration-change audit log" }; // Backward-compatible alias for the pre-M8 `audit-log` name. The alias keeps // full subcommand parity automatically; the deprecation warning is emitted by // the args[0] check in Program.cs. command.Aliases.Add(DeprecatedAlias); command.Add(BuildQuery(urlOption, formatOption, usernameOption, passwordOption)); return command; } private static Command BuildQuery(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption) { var userOption = new Option("--user") { Description = "Filter by username" }; var entityTypeOption = new Option("--entity-type") { Description = "Filter by entity type" }; var actionOption = new Option("--action") { Description = "Filter by action" }; var fromOption = new Option("--from") { Description = "Start date (ISO 8601)" }; var toOption = new Option("--to") { Description = "End date (ISO 8601)" }; var pageOption = new Option("--page") { Description = "Page number" }; pageOption.DefaultValueFactory = _ => 1; var pageSizeOption = new Option("--page-size") { Description = "Page size" }; pageSizeOption.DefaultValueFactory = _ => 50; var cmd = new Command("query") { Description = "Query audit log entries" }; cmd.Add(userOption); cmd.Add(entityTypeOption); cmd.Add(actionOption); cmd.Add(fromOption); cmd.Add(toOption); cmd.Add(pageOption); cmd.Add(pageSizeOption); cmd.SetAction(async (ParseResult result) => { var user = result.GetValue(userOption); var entityType = result.GetValue(entityTypeOption); var action = result.GetValue(actionOption); var from = result.GetValue(fromOption); var to = result.GetValue(toOption); var page = result.GetValue(pageOption); var pageSize = result.GetValue(pageSizeOption); return await CommandHelpers.ExecuteCommandAsync( result, urlOption, formatOption, usernameOption, passwordOption, new QueryAuditLogCommand(user, entityType, action, from, to, page, pageSize)); }); return cmd; } }