java.lang.Object
org.sentrysoftware.metricshub.engine.strategy.utils.OsCommandHelper

public class OsCommandHelper extends Object
Utility class for handling OS commands, including local and remote execution.
  • Field Details

  • Constructor Details

    • OsCommandHelper

      public OsCommandHelper()
  • Method Details

    • createOsCommandEmbeddedFiles

      public static Map<String,File> createOsCommandEmbeddedFiles(@NonNull @NonNull String commandLine, SudoInformation sudoInformation, @NonNull @NonNull Map<Integer,EmbeddedFile> commandLineEmbeddedFiles, @NonNull @NonNull BiFunction<String,String,File> tempFileCreator) throws IOException
      Create the temporary embedded files in the given command line.
      Parameters:
      commandLine - The command line to process.
      sudoInformation - The Sudo Information of the Os Command configuration.
      commandLineEmbeddedFiles - A map of embedded files referenced in the command line.
      tempFileCreator - The function that creates a temporary file.
      Returns:
      A map with EmbeddedFile tags as keys and corresponding temporary File objects.
      Throws:
      IOException - If an error occurs during temp file creation.
    • createTempFileWithEmbeddedFileContent

      public static File createTempFileWithEmbeddedFileContent(EmbeddedFile embeddedFile, SudoInformation sudoInformation, BiFunction<String,String,File> tempFileCreator) throws IOException
      Create a temporary file with the content of the embeddedFile.
      Parameters:
      embeddedFile - EmbeddedFile instance used to write the file content (mandatory)
      sudoInformation - The Sudo Information of the Os Command configuration.
      tempFileCreator - The function that creates a temporary file.
      Returns:
      The File.
      Throws:
      IOException
    • createEmbeddedTempFile

      public static File createEmbeddedTempFile(String baseName, String extension)
      Create a temporary file with the given extension.
      The temporary file name is prefixed with "metricshub_embedded_" to easily identify and clean up files in case of issues, preventing potential filesystem overloads.
      Parameters:
      baseName - Base name of the file. (File name without extension).
      extension - File's name suffix (e.g. .bat)
      Returns:
      File instance
    • replaceSudo

      public static String replaceSudo(String text, SudoInformation sudoInformation)
      Replace the %{SUDO:xxx}% tag in the given text with the sudo command. The replacement is based on the configuration in the provided OsCommandConfiguration. If the useSudo configuration is enabled and the sudo command is associated with the specified file, it replaces the tag with the sudo command; otherwise, it replaces it with an empty string.
      Parameters:
      text - The text containing %{SUDO:xxx}% tags to be replaced.
      sudoInformation - The Sudo Information of the Os Command configuration.
      Returns:
      The text with %{SUDO:xxx}% tags replaced with the sudo command or empty string.
    • getFileNameFromSudoCommand

      public static Optional<String> getFileNameFromSudoCommand(@NonNull @NonNull String command)

      Get the file name of the sudo pattern from the command.

      Example:

      "%{SUDO:fileName}" return "fileName"

      Parameters:
      command - The command.
      Returns:
      An Optional with The file name if found otherwise an empty optional.
    • toCaseInsensitiveRegex

      public static String toCaseInsensitiveRegex(String host)
      Convert a string to be searched in a case insensitive regex.
      Parameters:
      host - The string to searched. (mandatory)
      Returns:
      The case insensitive regex for this string.