1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.cli;
7   
8   import gov.nist.secauto.metaschema.cli.commands.MetaschemaCommands;
9   import gov.nist.secauto.metaschema.cli.processor.CLIProcessor;
10  import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
11  import gov.nist.secauto.metaschema.cli.processor.command.CommandService;
12  import gov.nist.secauto.metaschema.core.MetaschemaConstants;
13  import gov.nist.secauto.metaschema.core.MetaschemaJavaVersion;
14  import gov.nist.secauto.metaschema.core.model.MetaschemaVersion;
15  import gov.nist.secauto.metaschema.core.util.IVersionInfo;
16  
17  import java.util.LinkedHashMap;
18  import java.util.Map;
19  
20  import edu.umd.cs.findbugs.annotations.NonNull;
21  
22  /**
23   * The main entry point for the CLI application.
24   */
25  @SuppressWarnings("PMD.ShortClassName")
26  public final class CLI {
27    /**
28     * The main command line entry point.
29     *
30     * @param args
31     *          the command line arguments
32     */
33    public static void main(String[] args) {
34      System.exit(runCli(args).getExitCode().getStatusCode());
35    }
36  
37    /**
38     * Execute a command line.
39     *
40     * @param args
41     *          the command line arguments
42     * @return the execution result
43     */
44    @NonNull
45    public static ExitStatus runCli(String... args) {
46      System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
47  
48      @SuppressWarnings("PMD.UseConcurrentHashMap")
49      Map<String, IVersionInfo> versions = new LinkedHashMap<>();
50      versions.put(CLIProcessor.COMMAND_VERSION, new MetaschemaJavaVersion());
51      versions.put(MetaschemaConstants.METASCHEMA_NAMESPACE, new MetaschemaVersion());
52  
53      CLIProcessor processor = new CLIProcessor("metaschema-cli", versions);
54      MetaschemaCommands.COMMANDS.forEach(processor::addCommandHandler);
55  
56      CommandService.getInstance().getCommands().stream().forEach(command -> {
57        assert command != null;
58        processor.addCommandHandler(command);
59      });
60      return processor.process(args);
61    }
62  
63    private CLI() {
64      // disable construction
65    }
66  }