Class GOSInterface

java.lang.Object
com.glitchybyte.glib.process.GOSInterface
Direct Known Subclasses:
GOSLinux, GOSUnknown, GOSWindows

public abstract class GOSInterface extends Object
Singleton instance of an OS interface. The instance will be applicable to the currently running OS.

It has been tested on macOS, Linux, and Windows. But Windows needs an external app for proper signal (e.g., SIGINT) sending, if needed. More information in GOSWindows.

  • Field Details

    • instance

      public static final GOSInterface instance
      The instance.
    • SIGINT

      public static final sun.misc.Signal SIGINT
      INT signal.
    • osType

      public final GOSType osType
      OS type.
  • Constructor Details

    • GOSInterface

      protected GOSInterface(GOSType osType)
      Creates OS interface for the given OS type.
      Parameters:
      osType - OS type from which we want to create an interface.
  • Method Details

    • isSuccessfulExitCode

      public boolean isSuccessfulExitCode(Integer exitCode)
      Determines if the given exit code represents success.
      Parameters:
      exitCode - Exit code as returned by GProcess.
      Returns:
      True if exitCode represents success.
    • makeCommand

      public String[] makeCommand(String command)
      Convenience method to convert a string into an array for proper command array parameter.
      Parameters:
      command - Command string.
      Returns:
      An array containing the command string.
      Throws:
      IllegalArgumentException - If command has mismatched quotes.
    • getShellCommand

      public abstract String[] getShellCommand(String command)
      Returns a command array ready to call the shell with the given script.
      Parameters:
      command - Shell command with args.
      Returns:
      A new command string.
    • createProcessBuilder

      public ProcessBuilder createProcessBuilder(String[] command, Path dir)
      Creates a process builder.
      Parameters:
      command - Command string.
      dir - Starting directory. If null, it will inherit the current process current directory.
      Returns:
      A process builder.
    • execute

      public Integer execute(ProcessBuilder pb, List<String> output)
      Spawns and executes the given process, captures output into the given list, and waits until it is done.

      This is useful for immediate short-lived commands.

      Parameters:
      pb - Process builder containing the command.
      output - Output container. If null, output will not be captured.
      Returns:
      Process exit code.
    • execute

      public Integer execute(String[] command, Path dir, List<String> output)
      Spawns and executes the given command, captures output into the given list, and waits until it is done.
      Parameters:
      command - Command string.
      dir - Starting directory. If null, it will inherit the current process current directory.
      output - Output container. If null, output will not be captured.
      Returns:
      Process exit code.
    • execute

      public Integer execute(String[] command, Path dir)
      Spawns and executes the given command and waits until it is done.
      Parameters:
      command - Command string.
      dir - Starting directory. If null, it will inherit the current process current directory.
      Returns:
      Process exit code.
    • execute

      public Integer execute(String[] command)
      Spawns and executes the given command and waits until it is done.
      Parameters:
      command - Command string.
      Returns:
      Process exit code.
    • executeWithResult

      public GProcessResult executeWithResult(String[] command, Path dir)
      Spawns and executes the given command and waits until it is done.
      Parameters:
      command - Command string.
      dir - Starting directory. If null, it will inherit the current process current directory.
      Returns:
      A process result object with output and exit code.
    • executeWithResult

      public GProcessResult executeWithResult(String[] command)
      Spawns and executes the given command and waits until it is done.
      Parameters:
      command - Command string.
      Returns:
      A process result object with output and exit code.
    • getSignalCommand

      public abstract String[] getSignalCommand(sun.misc.Signal signal, long pid)
      Returns the command array that will send the given signal to the pid.
      Parameters:
      signal - Signal to send.
      pid - Pid of process.
      Returns:
      Command array.
    • sendSignal

      public Integer sendSignal(sun.misc.Signal signal, long pid)
      Sends signal to the given pid.
      Parameters:
      signal - Signal to send.
      pid - Pid of process.
      Returns:
      Process exit code.
    • sendSignalINT

      public Integer sendSignalINT(long pid)
      Sends SIGINT to the given pid.
      Parameters:
      pid - Pid of process.
      Returns:
      Process exit code.