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;
009import gov.nist.secauto.metaschema.core.util.ObjectUtils;
010
011import org.apache.commons.cli.CommandLine;
012
013import edu.umd.cs.findbugs.annotations.NonNull;
014
015/**
016 * A base class for implementation that perform the operation supported by a
017 * command.
018 */
019public abstract class AbstractCommandExecutor implements ICommandExecutor {
020  @NonNull
021  private final CallingContext callingContext;
022  @NonNull
023  private final CommandLine commandLine;
024
025  /**
026   * Construct a new command executor.
027   *
028   * @param callingContext
029   *          the context of the command execution
030   * @param commandLine
031   *          the parsed command line details
032   */
033  protected AbstractCommandExecutor(
034      @NonNull CallingContext callingContext,
035      @NonNull CommandLine commandLine) {
036    this.callingContext = callingContext;
037    this.commandLine = commandLine;
038  }
039
040  /**
041   * Get the context of the command execution, which provides access to the
042   * execution environment needed for command processing.
043   *
044   * @return the context
045   */
046  @NonNull
047  protected CallingContext getCallingContext() {
048    return callingContext;
049  }
050
051  /**
052   * Get the parsed command line details containing the command options and
053   * arguments provided by the user during execution.
054   *
055   * @return the cli details
056   */
057  @NonNull
058  protected CommandLine getCommandLine() {
059    return commandLine;
060  }
061
062  @Override
063  public abstract void execute() throws CommandExecutionException;
064
065  /**
066   * Get the command associated with this execution.
067   *
068   * @return the command
069   */
070  @NonNull
071  protected ICommand getCommand() {
072    return ObjectUtils.requireNonNull(getCallingContext().getTargetCommand());
073  }
074}