Class ShellExecutor

java.lang.Object
org.variantsync.diffdetective.shell.ShellExecutor

public class ShellExecutor extends Object
This class can execute ShellCommands in a specific working directory and redirect their output to a Java function.
Author:
Alexander Schultheiß
  • Field Details

  • Constructor Details

    • ShellExecutor

      public ShellExecutor(Consumer<String> outputReader, Consumer<String> errorReader)
      Constructs an executor of ShellCommands in the current working directory. The current working directory is the project/Git root by default when started through maven but this can be configured and is part of the contract between the caller of this function and the caller of DiffDetective.
      Parameters:
      outputReader - will be provided the stdout of the executed command
      errorReader - will be provided the stderr of the executed command
    • ShellExecutor

      public ShellExecutor(Consumer<String> outputReader, Consumer<String> errorReader, Path workDir)
      Constructs an executor of ShellCommands in the current working directory.
      Parameters:
      outputReader - will be provided the stdout of the executed command
      errorReader - will be provided the stderr of the executed command
      workDir - the default working directory for the executed commands
  • Method Details

    • execute

      public List<String> execute(ShellCommand command) throws ShellException
      Execute command in the default working directory. the default working directory is the directory given in the constructor or the current working directory of this process of not given.
      Parameters:
      command - the command to execute
      Throws:
      ShellException - if the executable in command can't be executed or it exits with an error exit code
    • execute

      public List<String> execute(ShellCommand command, Path executionDir) throws ShellException
      Execute command in the given working directory. The default working directory (given in the constructor) isn't used.
      Parameters:
      command - the command to execute
      executionDir - the directory in which command is executed (working directory)
      Throws:
      ShellException - if the executable in command can't be executed or it exits with an error exit code
    • collectOutput

      private Runnable collectOutput(InputStream inputStream, Consumer<String> consumer)
      Feed the lines read from inputStream to consumer.