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