From 970258c2c83291d65a512381d2688e25eb1125f0 Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Fri, 1 Nov 2024 10:57:12 -0400 Subject: [PATCH] Started refactoring CLI commands to use more shared code. --- .../cli/processor/CLIProcessor.java | 85 +++++----- .../command/AbstractTerminalCommand.java | 24 ++- .../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 | 27 +-- .../AbstractValidateContentCommand.java | 88 ++++------ .../ConvertContentUsingModuleCommand.java | 6 +- .../cli/commands/GenerateSchemaCommand.java | 31 +--- .../cli/commands/MetaschemaCommands.java | 156 +++++++++++++++++- .../ValidateContentUsingModuleCommand.java | 13 +- .../cli/commands/ValidateModuleCommand.java | 7 +- .../metapath/EvaluateMetapathCommand.java | 4 +- 17 files changed, 346 insertions(+), 214 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..c56a3b7d4 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 @@ -11,6 +11,7 @@ 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 +36,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 +164,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 +184,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() { @@ -230,63 +243,43 @@ public class CallingContext { @NonNull private final List