001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.oscal.tools.cli.core;
007
008import gov.nist.secauto.metaschema.cli.processor.CLIProcessor;
009import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
010import gov.nist.secauto.metaschema.core.MetaschemaJavaVersion;
011import gov.nist.secauto.metaschema.core.model.MetaschemaVersion;
012import gov.nist.secauto.metaschema.core.util.CollectionUtil;
013import gov.nist.secauto.metaschema.core.util.IVersionInfo;
014import gov.nist.secauto.oscal.lib.LibOscalVersion;
015import gov.nist.secauto.oscal.lib.OscalVersion;
016import gov.nist.secauto.oscal.tools.cli.core.commands.ConvertCommand;
017import gov.nist.secauto.oscal.tools.cli.core.commands.ListAllowedValuesCommand;
018import gov.nist.secauto.oscal.tools.cli.core.commands.ResolveCommand;
019import gov.nist.secauto.oscal.tools.cli.core.commands.ValidateCommand;
020import gov.nist.secauto.oscal.tools.cli.core.commands.assessmentplan.AssessmentPlanCommand;
021import gov.nist.secauto.oscal.tools.cli.core.commands.assessmentresults.AssessmentResultsCommand;
022import gov.nist.secauto.oscal.tools.cli.core.commands.catalog.CatalogCommand;
023import gov.nist.secauto.oscal.tools.cli.core.commands.componentdefinition.ComponentDefinitionCommand;
024import gov.nist.secauto.oscal.tools.cli.core.commands.metaschema.MetaschemaCommand;
025import gov.nist.secauto.oscal.tools.cli.core.commands.poam.PlanOfActionsAndMilestonesCommand;
026import gov.nist.secauto.oscal.tools.cli.core.commands.profile.ProfileCommand;
027import gov.nist.secauto.oscal.tools.cli.core.commands.ssp.SystemSecurityPlanCommand;
028
029import java.util.LinkedHashMap;
030import java.util.Map;
031
032import edu.umd.cs.findbugs.annotations.NonNull;
033
034/**
035 * Provides the main entry point for executing the command line interface.
036 */
037@SuppressWarnings("PMD.ShortClassName")
038public final class CLI {
039  /**
040   * Executes the CLI and handled the exit code.
041   *
042   * @param args
043   *          the CLI arguments
044   */
045  public static void main(String[] args) {
046    System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
047
048    ExitStatus status = runCli(args);
049    int exitCode = status.getExitCode().getStatusCode();
050    System.exit(exitCode);
051  }
052
053  /**
054   * Executes the CLI.
055   *
056   * @param args
057   *          the CLI arguments
058   * @return the result of executing the CLI
059   */
060  @NonNull
061  public static ExitStatus runCli(String... args) {
062    @SuppressWarnings("PMD.UseConcurrentHashMap")
063    Map<String, IVersionInfo> versions = new LinkedHashMap<>();
064    versions.put(CLIProcessor.COMMAND_VERSION, new OscalCliVersion());
065    versions.put("https://github.com/usnistgov/liboscal-java", new LibOscalVersion());
066    versions.put("https://github.com/usnistgov/OSCAL", new OscalVersion());
067    versions.put("https://github.com/usnistgov/metaschema-java", new MetaschemaJavaVersion());
068    versions.put("https://github.com/usnistgov/metaschema", new MetaschemaVersion());
069
070    CLIProcessor processor = new CLIProcessor("oscal-cli", CollectionUtil.unmodifiableMap(versions));
071    processor.addCommandHandler(new CatalogCommand());
072    processor.addCommandHandler(new ProfileCommand());
073    processor.addCommandHandler(new ComponentDefinitionCommand());
074    processor.addCommandHandler(new SystemSecurityPlanCommand());
075    processor.addCommandHandler(new AssessmentPlanCommand());
076    processor.addCommandHandler(new AssessmentResultsCommand());
077    processor.addCommandHandler(new PlanOfActionsAndMilestonesCommand());
078    processor.addCommandHandler(new MetaschemaCommand());
079    processor.addCommandHandler(new ValidateCommand());
080    processor.addCommandHandler(new ConvertCommand());
081    processor.addCommandHandler(new ResolveCommand());
082    processor.addCommandHandler(new ListAllowedValuesCommand());
083    return processor.process(args);
084  }
085
086  private CLI() {
087    // disable construction
088  }
089}