Skip to content

Commit

Permalink
Refactored CLI commands to simplify the command pathways for conversi…
Browse files Browse the repository at this point in the history
…on, profile resolution, and validations to make these commands more CI/CD friendly.
  • Loading branch information
david-waltermire committed Jun 9, 2024
1 parent 0a6d5bf commit 8b2e47e
Show file tree
Hide file tree
Showing 26 changed files with 742 additions and 321 deletions.
6 changes: 6 additions & 0 deletions src/main/java/gov/nist/secauto/oscal/tools/cli/core/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.LibOscalVersion;
import gov.nist.secauto.oscal.lib.OscalVersion;
import gov.nist.secauto.oscal.tools.cli.core.commands.ConvertCommand;
import gov.nist.secauto.oscal.tools.cli.core.commands.ResolveCommand;
import gov.nist.secauto.oscal.tools.cli.core.commands.ValidateCommand;
import gov.nist.secauto.oscal.tools.cli.core.commands.assessmentplan.AssessmentPlanCommand;
import gov.nist.secauto.oscal.tools.cli.core.commands.assessmentresults.AssessmentResultsCommand;
import gov.nist.secauto.oscal.tools.cli.core.commands.catalog.CatalogCommand;
Expand Down Expand Up @@ -75,6 +78,9 @@ public static ExitStatus runCli(String... args) {
processor.addCommandHandler(new AssessmentResultsCommand());
processor.addCommandHandler(new PlanOfActionsAndMilestonesCommand());
processor.addCommandHandler(new MetaschemaCommand());
processor.addCommandHandler(new ValidateCommand());
processor.addCommandHandler(new ConvertCommand());
processor.addCommandHandler(new ResolveCommand());
return processor.process(args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,31 @@
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.oscal.tools.cli.core.commands.oscal;
package gov.nist.secauto.oscal.tools.cli.core.commands;

import gov.nist.secauto.metaschema.cli.commands.AbstractConvertSubcommand;
import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
import gov.nist.secauto.metaschema.databind.IBindingContext;
import gov.nist.secauto.metaschema.databind.io.Format;
import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
import gov.nist.secauto.oscal.lib.OscalBindingContext;

import org.apache.commons.cli.CommandLine;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Writer;
import java.net.URI;

import edu.umd.cs.findbugs.annotations.NonNull;

public abstract class AbstractOscalConvertSubcommand
extends AbstractConvertSubcommand {
private static final Logger LOGGER = LogManager.getLogger(AbstractOscalConvertSubcommand.class);

@NonNull
public abstract Class<?> getOscalClass();
Expand All @@ -62,8 +73,17 @@ protected IBindingContext getBindingContext() {
}

@Override
protected Class<?> getLoadedClass() {
return getOscalClass();
public ExitStatus execute() {
LOGGER.atWarn().log("This command path is deprecated. Please use 'convert'.");

return super.execute();
}

@Override
protected void handleConversion(URI source, Format toFormat, Writer writer, IBoundLoader loader)
throws FileNotFoundException, IOException {
Class<?> clazz = getOscalClass();
loader.convert(source, writer, toFormat, clazz);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.oscal.tools.cli.core.commands.oscal;
package gov.nist.secauto.oscal.tools.cli.core.commands;

import gov.nist.secauto.metaschema.cli.commands.AbstractValidateContentCommand;
import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
Expand All @@ -39,6 +39,7 @@
import org.json.JSONObject;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Set;

Expand All @@ -60,10 +61,10 @@ public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine c
return new OscalCommandExecutor(callingContext, commandLine);
}

private final class OscalCommandExecutor
protected class OscalCommandExecutor
extends AbstractValidationCommandExecutor {

private OscalCommandExecutor(
protected OscalCommandExecutor(
@NonNull CallingContext callingContext,
@NonNull CommandLine commandLine) {
super(callingContext, commandLine);
Expand All @@ -85,13 +86,13 @@ protected IBindingContext getBindingContext(@NonNull Set<IConstraintSet> constra

@Override
@NonNull
public List<Source> getXmlSchemas() throws IOException {
public List<Source> getXmlSchemas(URL targetResource) throws IOException {
return getOscalXmlSchemas();
}

@Override
@NonNull
public JSONObject getJsonSchema() throws IOException {
public JSONObject getJsonSchema(JSONObject json) throws IOException {
return getOscalJsonSchema();
}
}
Expand Down
Loading

0 comments on commit 8b2e47e

Please sign in to comment.