001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.cli;
007
008import gov.nist.secauto.metaschema.cli.commands.MetaschemaCommands;
009import gov.nist.secauto.metaschema.cli.processor.CLIProcessor;
010import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
011import gov.nist.secauto.metaschema.cli.processor.command.CommandService;
012import gov.nist.secauto.metaschema.core.MetaschemaJavaVersion;
013import gov.nist.secauto.metaschema.core.model.MetaschemaVersion;
014import gov.nist.secauto.metaschema.core.util.IVersionInfo;
015import gov.nist.secauto.metaschema.core.util.ObjectUtils;
016
017import java.util.LinkedHashMap;
018import java.util.Map;
019
020import edu.umd.cs.findbugs.annotations.NonNull;
021
022@SuppressWarnings("PMD.ShortClassName")
023public final class CLI {
024  /**
025   * The main command line entry point.
026   *
027   * @param args
028   *          the command line arguments
029   */
030  public static void main(String[] args) {
031    System.exit(runCli(args).getExitCode().getStatusCode());
032  }
033
034  /**
035   * Execute a command line.
036   *
037   * @param args
038   *          the command line arguments
039   * @return the execution result
040   */
041  @NonNull
042  public static ExitStatus runCli(String... args) {
043    System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
044
045    @SuppressWarnings("serial") Map<String, IVersionInfo> versions = ObjectUtils.notNull(
046        new LinkedHashMap<>() {
047          {
048            put(CLIProcessor.COMMAND_VERSION, new MetaschemaJavaVersion());
049            put("http://csrc.nist.gov/ns/oscal/metaschema/1.0", new MetaschemaVersion());
050          }
051        });
052    CLIProcessor processor = new CLIProcessor("metaschema-cli", versions);
053    MetaschemaCommands.COMMANDS.forEach(processor::addCommandHandler);
054
055    CommandService.getInstance().getCommands().stream().forEach(command -> {
056      assert command != null;
057      processor.addCommandHandler(command);
058    });
059    return processor.process(args);
060  }
061
062  private CLI() {
063    // disable construction
064  }
065}