org.whitesource.utils.cli.Cli Maven / Gradle / Ivy
package org.whitesource.utils.cli;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whitesource.utils.Constants;
import org.whitesource.utils.OsDetector;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class Cli {
private final Logger logger = LoggerFactory.getLogger(Cli.class);
private Integer timeoutProcessMinutes;
private Long timeoutReadLineSeconds;
private boolean errorInProcess;
public Cli(){}
public Cli(long timeoutReadLineSeconds){
this.timeoutReadLineSeconds = timeoutReadLineSeconds;
}
public Cli(int timeoutProcessMinutes){
this.timeoutProcessMinutes = timeoutProcessMinutes;
}
public List runCmd(String rootDirectory, String[] params){
List lines = new LinkedList<>();
try {
CommandLineProcess commandLineProcess = new CommandLineProcess(rootDirectory, params);
setTimeouts(commandLineProcess);
lines = commandLineProcess.executeProcess();
errorInProcess = commandLineProcess.isErrorInProcess();
if (!errorInProcess) {
return lines;
}
} catch (IOException e) {
printException(e, params, rootDirectory);
}
return lines;
}
public boolean runCmdWithoutOutput(String rootDirectory, String[] params){
try {
CommandLineProcess commandLineProcess = new CommandLineProcess(rootDirectory, params);
setTimeouts(commandLineProcess);
commandLineProcess.executeProcessWithoutOutput();
if (!commandLineProcess.isErrorInProcess()) {
return true;
}
} catch (IOException e) {
printException(e, params, rootDirectory);
}
return false;
}
public boolean runCmdWithErrorOutput(String rootDirectory, String[] params){
try {
CommandLineProcess commandLineProcess = new CommandLineProcess(rootDirectory, params);
setTimeouts(commandLineProcess);
commandLineProcess.executeProcessWithErrorOutput();
if (!commandLineProcess.isErrorInProcess()) {
return true;
}
} catch (IOException e){
printException(e, params, rootDirectory);
}
return false;
}
public String[] getCommandParams(String command, String ... params){
if (params.length > 1){
return getCommandParamsArray(command, params);
} else if (params.length == 1 && params[0].contains(Constants.WHITESPACE)){
return getCommandParamsArray(command, params[0].split(Constants.WHITESPACE));
}
String joinedParams = String.join(Constants.WHITESPACE, params);
if (OsDetector.isWindows()) {
return new String[] {Constants.CMD, Constants.C_CHAR_WINDOWS, command, joinedParams};
}
return new String[] {command, joinedParams};
}
public boolean isErrorInProcess() {
return errorInProcess;
}
private String[] getCommandParamsArray(String command, String[] params){
String[] output;
if (OsDetector.isWindows()) {
output = new String[3 + params.length];
output[0] = Constants.CMD;
output[1] = Constants.C_CHAR_WINDOWS;
output[2] = command;
for (int i = 0; i < params.length; i++){
output[i + 3] = params[i];
}
} else {
output = new String[1 + params.length];
output[0] = command;
for (int i = 0; i < params.length; i++){
output[i + 1] = params[i];
}
}
return output;
}
private void setTimeouts(CommandLineProcess commandLineProcess){
if (this.timeoutProcessMinutes != null){
commandLineProcess.setTimeoutProcessMinutes(timeoutProcessMinutes);
}
if (this.timeoutReadLineSeconds != null){
commandLineProcess.setTimeoutReadLineSeconds(this.timeoutReadLineSeconds);
}
}
private void printException(Exception e, String[] params, String directory){
logger.warn("Error getting dependencies after running {} on {}, {}" , params , directory, e.getMessage());
logger.debug("Error: {}", e);
}
}