- java.lang.Object
-
- com.gluonhq.substrate.util.ProcessRunner
-
public class ProcessRunner extends java.lang.ObjectUtility class to run processes based on command line arguments by wrapping a .
-
-
Constructor Summary
Constructors Constructor Description ProcessRunner(java.lang.String... args)Constructor, allowing some command line arguments
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddArg(java.lang.String arg)Adds a command line argument to the list of existing list of command line argumentsvoidaddArgs(java.lang.String... args)Adds a varargs list of arguments to the existing list of command line of argumentsvoidaddArgs(java.util.Collection<java.lang.String> args)Adds a collection of arguments to the existing list of command line of argumentsvoidaddSecretArg(java.lang.String arg)Adds a command line argument to the list of existing list of command line arguments, marking it as secret argument, in order to avoid loggingvoidaddToEnv(java.lang.String key, java.lang.String value)Adds a pair (key, value) to the environment map of the processstatic java.lang.IntegerexecuteWithFeedback(java.lang.String name, java.lang.String... args)Executes a process while printing "." every second the process is running.java.lang.StringgetCmd()java.util.List<java.lang.String>getCmdList()java.lang.StringgetLastResponse()Gets the last line of the output processjava.lang.StringgetResponse()Gets the response of the process as single stringjava.util.List<java.lang.String>getResponses()Gets the response of the process as list of linesintrunProcess(java.lang.String processName)Runs a process with a given set of command line argumentsintrunProcess(java.lang.String processName, java.io.File workingDirectory)Runs a process with a given set of command line arguments, in a given working directory.static java.lang.StringrunProcessForSingleOutput(java.lang.String name, java.lang.String... args)Static method that can be used to process a given command line, returning the output of the process as a single string, or null if it failed.booleanrunTimedProcess(java.lang.String processName, long timeout)Runs a process with a given set of command line arguments within a given time framebooleanrunTimedProcess(java.lang.String processName, java.io.File workingDirectory, long timeout)Runs a process with a given set of command line arguments, in a given working directory, within a given time framestatic voidsetConsoleProcessLog(boolean value)Sets true if the processes are logged, not only to a file, but also to console.voidsetInfo(boolean info)When set to true, it will log with Level.INFO the output during the process.voidsetInteractive(boolean interactive)When set to true, it will enable user interaction during the process.voidsetLogToFile(boolean logToFile)When set to true, it will log to a file the command line arguments, the output and the result of the process.static voidsetProcessLogPath(java.nio.file.Path path)Sets the path where the process logs will be created.voidshowSevereMessage(boolean showSevere)When set to true, a message with Level.SEVERE will be logged in case the process fails.
-
-
-
Method Detail
-
setProcessLogPath
public static void setProcessLogPath(java.nio.file.Path path) throws java.io.IOExceptionSets the path where the process logs will be created. If the path doesn't exist, it will be created. This should be called once. If not set, the process won't be logged.- Parameters:
path- the path where the process logs will be created- Throws:
java.io.IOException
-
setConsoleProcessLog
public static void setConsoleProcessLog(boolean value)
Sets true if the processes are logged, not only to a file, but also to console. Useful, for instance, in CI environments without access to log files. This should be called once.- Parameters:
value- true if process is logged to console, false by default
-
setInfo
public void setInfo(boolean info)
When set to true, it will log with Level.INFO the output during the process. By default is false, and uses Level.DEBUG- Parameters:
info- a boolean that sets the log level of the process output
-
showSevereMessage
public void showSevereMessage(boolean showSevere)
When set to true, a message with Level.SEVERE will be logged in case the process fails. By default is true.- Parameters:
showSevere- a boolean that allows showing or not a severe message
-
setInteractive
public void setInteractive(boolean interactive)
When set to true, it will enable user interaction during the process. By default is false- Parameters:
interactive- a boolean that sets the interactive mode
-
setLogToFile
public void setLogToFile(boolean logToFile)
When set to true, it will log to a file the command line arguments, the output and the result of the process. By default it is false, but the log will be created regardless this value for any failing process.- Parameters:
logToFile- if true will log the process to a file
-
addArg
public void addArg(java.lang.String arg)
Adds a command line argument to the list of existing list of command line arguments- Parameters:
arg- a string passed to the command line arguments
-
addSecretArg
public void addSecretArg(java.lang.String arg)
Adds a command line argument to the list of existing list of command line arguments, marking it as secret argument, in order to avoid logging- Parameters:
arg- a string passed to the command line arguments
-
addArgs
public void addArgs(java.lang.String... args)
Adds a varargs list of arguments to the existing list of command line of arguments- Parameters:
args- varargs list of arguments
-
addArgs
public void addArgs(java.util.Collection<java.lang.String> args)
Adds a collection of arguments to the existing list of command line of arguments- Parameters:
args- a collection of arguments
-
getCmd
public java.lang.String getCmd()
- Returns:
- the command line of arguments as a string
-
getCmdList
public java.util.List<java.lang.String> getCmdList()
- Returns:
- the current list of command line arguments
-
addToEnv
public void addToEnv(java.lang.String key, java.lang.String value)Adds a pair (key, value) to the environment map of the process- Parameters:
key- a string with the environmental variable namevalue- a string with the environmental variable value
-
runProcess
public int runProcess(java.lang.String processName) throws java.io.IOException, java.lang.InterruptedExceptionRuns a process with a given set of command line arguments- Parameters:
processName- the name of the process- Returns:
- 0 if the process ends successfully, non-zero values indicate a failure
- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
runProcess
public int runProcess(java.lang.String processName, java.io.File workingDirectory) throws java.io.IOException, java.lang.InterruptedExceptionRuns a process with a given set of command line arguments, in a given working directory.- Parameters:
processName- the name of the processworkingDirectory- a file with the working directory of the process- Returns:
- 0 if the process ends successfully, non-zero values indicate a failure
- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
runTimedProcess
public boolean runTimedProcess(java.lang.String processName, long timeout) throws java.io.IOException, java.lang.InterruptedExceptionRuns a process with a given set of command line arguments within a given time frame- Parameters:
processName- the name of the processtimeout- the maximum time allowed to run the process- Returns:
- true if the process ended successfully, false otherwise
- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
runTimedProcess
public boolean runTimedProcess(java.lang.String processName, java.io.File workingDirectory, long timeout) throws java.io.IOException, java.lang.InterruptedExceptionRuns a process with a given set of command line arguments, in a given working directory, within a given time frame- Parameters:
processName- the name of the processworkingDirectory- a file with the working directory of the processtimeout- the maximum time allowed to run the process- Returns:
- true if the process ended successfully, false otherwise
- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
getResponse
public java.lang.String getResponse()
Gets the response of the process as single string- Returns:
- a single string with the whole output of the process
-
getResponses
public java.util.List<java.lang.String> getResponses()
Gets the response of the process as list of lines- Returns:
- a list with all the lines of the output
-
getLastResponse
public java.lang.String getLastResponse()
Gets the last line of the output process- Returns:
- a string with the last line of the output
-
runProcessForSingleOutput
public static java.lang.String runProcessForSingleOutput(java.lang.String name, java.lang.String... args) throws java.io.IOException, java.lang.InterruptedExceptionStatic method that can be used to process a given command line, returning the output of the process as a single string, or null if it failed. It is convenient when the output of the process to be executed returns a single line- Parameters:
name- the name of the processargs- a varargs list of command line arguments- Returns:
- a string with the response of the process or null if it failed
- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
executeWithFeedback
public static java.lang.Integer executeWithFeedback(java.lang.String name, java.lang.String... args) throws java.io.IOException, java.lang.InterruptedExceptionExecutes a process while printing "." every second the process is running. The feedback is helpful for end user. A lack of feedback can lead to an impression that the process is stuck.- Parameters:
name- the name of the processargs- a varargs list of command line arguments- Returns:
- Integer result of the process
- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
-