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.io.PrintStream;
18  import java.util.LinkedHashMap;
19  import java.util.Map;
20  
21  import edu.umd.cs.findbugs.annotations.NonNull;
22  import edu.umd.cs.findbugs.annotations.Nullable;
23  
24  /**
25   * The main entry point for the CLI application.
26   */
27  @SuppressWarnings("PMD.ShortClassName")
28  public final class CLI {
29    /**
30     * The main command line entry point.
31     *
32     * @param args
33     *          the command line arguments
34     */
35    public static void main(String[] args) {
36      System.exit(runCli(args).getExitCode().getStatusCode());
37    }
38  
39    /**
40     * Execute a command line.
41     *
42     * @param args
43     *          the command line arguments
44     * @return the execution result
45     */
46    @NonNull
47    public static ExitStatus runCli(String... args) {
48      return runCli(null, args);
49    }
50  
51    /**
52     * Execute a command line with a custom output stream.
53     * <p>
54     * This method is useful for testing, allowing output to be captured instead of
55     * being written directly to the console.
56     *
57     * @param outputStream
58     *          the output stream to write to, or {@code null} to use the default
59     *          console
60     * @param args
61     *          the command line arguments
62     * @return the execution result
63     */
64    @NonNull
65    public static ExitStatus runCli(@Nullable PrintStream outputStream, String... args) {
66      System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
67  
68      @SuppressWarnings("PMD.UseConcurrentHashMap")
69      Map<String, IVersionInfo> versions = new LinkedHashMap<>();
70      versions.put(CLIProcessor.COMMAND_VERSION, new MetaschemaJavaVersion());
71      versions.put(MetaschemaConstants.METASCHEMA_NAMESPACE, new MetaschemaVersion());
72  
73      CLIProcessor processor = new CLIProcessor("metaschema-cli", versions, outputStream);
74      MetaschemaCommands.COMMANDS.forEach(processor::addCommandHandler);
75  
76      CommandService.getInstance().getCommands().stream().forEach(command -> {
77        assert command != null;
78        processor.addCommandHandler(command);
79      });
80      return processor.process(args);
81    }
82  
83    private CLI() {
84      // disable construction
85    }
86  }