Package com.glitchybyte.glib.process
Class GOSInterface
java.lang.Object
com.glitchybyte.glib.process.GOSInterface
- Direct Known Subclasses:
GOSLinux,GOSUnknown,GOSWindows
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final GOSInterfaceThe instance.final GOSTypeOS type.static final sun.misc.SignalINT signal. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedGOSInterface(GOSType osType) Creates OS interface for the given OS type. -
Method Summary
Modifier and TypeMethodDescriptioncreateProcessBuilder(String[] command, Path dir) Creates a process builder.execute(ProcessBuilder pb, List<String> output) Spawns and executes the given process, captures output into the given list, and waits until it is done.Spawns and executes the given command and waits until it is done.Spawns and executes the given command and waits until it is done.Spawns and executes the given command, captures output into the given list, and waits until it is done.executeWithResult(String[] command) Spawns and executes the given command and waits until it is done.executeWithResult(String[] command, Path dir) Spawns and executes the given command and waits until it is done.abstract String[]getShellCommand(String command) Returns a command array ready to call the shell with the given script.abstract String[]getSignalCommand(sun.misc.Signal signal, long pid) Returns the command array that will send the given signal to the pid.booleanisSuccessfulExitCode(Integer exitCode) Determines if the given exit code represents success.String[]makeCommand(String command) Convenience method to convert a string into an array for proper command array parameter.sendSignal(sun.misc.Signal signal, long pid) Sends signal to the given pid.sendSignalINT(long pid) Sends SIGINT to the given pid.
-
Field Details
-
instance
The instance. -
SIGINT
public static final sun.misc.Signal SIGINTINT signal. -
osType
OS type.
-
-
Constructor Details
-
GOSInterface
Creates OS interface for the given OS type.- Parameters:
osType- OS type from which we want to create an interface.
-
-
Method Details
-
isSuccessfulExitCode
Determines if the given exit code represents success.- Parameters:
exitCode- Exit code as returned by GProcess.- Returns:
- True if
exitCoderepresents success.
-
makeCommand
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
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
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
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
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
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
Spawns and executes the given command and waits until it is done.- Parameters:
command- Command string.- Returns:
- Process exit code.
-
executeWithResult
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
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
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
Sends signal to the given pid.- Parameters:
signal- Signal to send.pid- Pid of process.- Returns:
- Process exit code.
-
sendSignalINT
Sends SIGINT to the given pid.- Parameters:
pid- Pid of process.- Returns:
- Process exit code.
-