From e17bafc0d00f3e8b8140795d85d8530bca3587a2 Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Fri, 1 Nov 2024 10:57:12 -0400 Subject: [PATCH] Refactoring CLI commands to use more shared code. This reduces the complexity around command authoring a great deal. --- .../cli/processor/CLIProcessor.java | 237 ++++++----- .../command/AbstractCommandExecutor.java | 3 +- .../command/AbstractTerminalCommand.java | 27 +- .../processor/command/ICommandExecutor.java | 17 +- .../core/metapath/item/DefaultItemWriter.java | 2 +- .../validation/XmlSchemaContentValidator.java | 3 +- .../metaschema/core/util/UriUtils.java | 13 +- .../AbstractBindingModelContainerSupport.java | 18 + .../impl/AssemblyModelContainerSupport.java | 47 ++- .../metaschema/impl/BindingConstants.java | 1 + .../impl/ChoiceModelContainerSupport.java | 35 +- .../gov/nist/secauto/metaschema/cli/CLI.java | 2 +- .../commands/AbstractConvertSubcommand.java | 112 +---- .../AbstractValidateContentCommand.java | 162 ++------ .../ConvertContentUsingModuleCommand.java | 30 +- .../cli/commands/GenerateDiagramCommand.java | 118 ++---- .../cli/commands/GenerateSchemaCommand.java | 77 +--- .../cli/commands/MetaschemaCommands.java | 381 ++++++++++++++++-- .../ValidateContentUsingModuleCommand.java | 35 +- .../cli/commands/ValidateModuleCommand.java | 11 +- .../metapath/EvaluateMetapathCommand.java | 86 ++-- .../EvaluateMetapathSubCommandTest.java | 4 - 22 files changed, 782 insertions(+), 639 deletions(-) diff --git a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java index ff919cf54..17c015a00 100644 --- a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java +++ b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java @@ -7,10 +7,12 @@ import static org.fusesource.jansi.Ansi.ansi; +import gov.nist.secauto.metaschema.cli.processor.command.CommandExecutionException; import gov.nist.secauto.metaschema.cli.processor.command.CommandService; import gov.nist.secauto.metaschema.cli.processor.command.ExtraArgument; import gov.nist.secauto.metaschema.cli.processor.command.ICommand; import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.IVersionInfo; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -35,11 +37,10 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; -import java.util.Deque; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; import java.util.stream.Collectors; @@ -164,6 +165,13 @@ private ExitStatus parseCommand(String... args) { assert commandArgs != null; CallingContext callingContext = new CallingContext(commandArgs); + if (LOGGER.isDebugEnabled()) { + String commandChain = callingContext.getCalledCommands().stream() + .map(ICommand::getName) + .collect(Collectors.joining(" -> ")); + LOGGER.debug("Processing command chain: {}", commandChain); + } + ExitStatus status; // the first two arguments should be the and , where // is the object type @@ -177,10 +185,16 @@ private ExitStatus parseCommand(String... args) { return status; } + @NonNull protected final List getTopLevelCommands() { - List retval = Collections.unmodifiableList(commands); - assert retval != null; - return retval; + return CollectionUtil.unmodifiableList(commands); + } + + @NonNull + protected final Map getTopLevelCommandsByName() { + return ObjectUtils.notNull(getTopLevelCommands() + .stream() + .collect(Collectors.toUnmodifiableMap(ICommand::getName, Function.identity()))); } private static void handleNoColor() { @@ -200,7 +214,8 @@ public static void handleQuiet() { } protected void showVersion() { - @SuppressWarnings("resource") PrintStream out = AnsiConsole.out(); // NOPMD - not owner + @SuppressWarnings("resource") + PrintStream out = AnsiConsole.out(); // NOPMD - not owner getVersionInfos().values().stream().forEach(info -> { out.println(ansi() .bold().a(info.getName()).boldOff() @@ -230,63 +245,44 @@ public class CallingContext { @NonNull private final List