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.MetaschemaConstants;
013import gov.nist.secauto.metaschema.core.MetaschemaJavaVersion;
014import gov.nist.secauto.metaschema.core.model.MetaschemaVersion;
015import gov.nist.secauto.metaschema.core.util.IVersionInfo;
016
017import java.util.LinkedHashMap;
018import java.util.Map;
019
020import edu.umd.cs.findbugs.annotations.NonNull;
021
022/**
023 * The main entry point for the CLI application.
024 */
025@SuppressWarnings("PMD.ShortClassName")
026public final class CLI {
027  /**
028   * The main command line entry point.
029   *
030   * @param args
031   *          the command line arguments
032   */
033  public static void main(String[] args) {
034    System.exit(runCli(args).getExitCode().getStatusCode());
035  }
036
037  /**
038   * Execute a command line.
039   *
040   * @param args
041   *          the command line arguments
042   * @return the execution result
043   */
044  @NonNull
045  public static ExitStatus runCli(String... args) {
046    System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
047
048    @SuppressWarnings("PMD.UseConcurrentHashMap")
049    Map<String, IVersionInfo> versions = new LinkedHashMap<>();
050    versions.put(CLIProcessor.COMMAND_VERSION, new MetaschemaJavaVersion());
051    versions.put(MetaschemaConstants.METASCHEMA_NAMESPACE, new MetaschemaVersion());
052
053    CLIProcessor processor = new CLIProcessor("metaschema-cli", versions);
054    MetaschemaCommands.COMMANDS.forEach(processor::addCommandHandler);
055
056    CommandService.getInstance().getCommands().stream().forEach(command -> {
057      assert command != null;
058      processor.addCommandHandler(command);
059    });
060    return processor.process(args);
061  }
062
063  private CLI() {
064    // disable construction
065  }
066}