001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.cli.processor.command;
007
008import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
009
010import org.apache.commons.cli.CommandLine;
011
012import edu.umd.cs.findbugs.annotations.NonNull;
013
014/**
015 * An abstract base class that implements the {@link ICommandExecutor}
016 * interface, providing common functionality for command execution
017 * implementations. Concrete subclasses must implement the {@link #execute()}
018 * method to define specific command behavior.
019 */
020@FunctionalInterface
021public interface ICommandExecutor {
022  /**
023   * Execute the command operation.
024   *
025   * @throws CommandExecutionException
026   *           if an error occurred while executing the command operation
027   */
028  void execute() throws CommandExecutionException;
029
030  /**
031   * Create a new command executor.
032   *
033   * @param callingContext
034   *          the context of the command execution
035   * @param commandLine
036   *          the parsed command line details
037   * @param function
038   *          a function that accepts a calling context and command line
039   *          information
040   * @return the executor instance
041   */
042  @NonNull
043  static ICommandExecutor using(
044      @NonNull CallingContext callingContext,
045      @NonNull CommandLine commandLine,
046      @NonNull ExecutionFunction function) {
047    return () -> function.execute(callingContext, commandLine);
048  }
049
050  /**
051   * This functional interface represents a method that is used to execute a
052   * command operation.
053   */
054  @FunctionalInterface
055  interface ExecutionFunction {
056    /**
057     * Execute a command operation.
058     *
059     * @param callingContext
060     *          the context of the command execution
061     * @param commandLine
062     *          the parsed command line details
063     * @throws CommandExecutionException
064     *           if an error occurred while executing the command operation
065     */
066    void execute(
067        @NonNull CallingContext callingContext,
068        @NonNull CommandLine commandLine) throws CommandExecutionException;
069  }
070}