I created a method (apiType
) that executes a command on a linux server. The result of the method should be used in my main class for executing a class or other class, depending if the result is "app2" or "app3".
The result of the linux command (cat /opt/pcu/bin/version | grep PRODUCT | cut -d' ' -f2
) is either "app2" or "app3"
So my question is how can I call and use the result of the method in my main class?
OR what I do wrong or I miss in the code?
public class Api {
private final static String version = "v2";
private final static String path = FileSystems.getDefault().getPath(".").toAbsolutePath().getParent().toString();
public String apiType() throws Exception {
try {
Exec exec = new Exec();
exec.setCommand("cat /opt/pcu/bin/version | grep PRODUCT | cut -d' ' -f2");
exec.setLogPath(path);
exec.run("apiType");
exec.join(200);
Vector<String> output = exec.getOutput(false);
return output.get(0); //this should return "app2" or "app3"?!
} catch (Exception e) {
System.out.println("Failed to run command");
throw e;
}
}
public static void main(String[] args) {
if (args.length == 0) {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy/hh:mm:ss");
System.out.println(
"\n\n==================================== TestAPI program ====================================\n");
System.out.println("Date: " + dateFormat.format(date.getTime()) + " " + version);
System.out.println(
"\n==============================================================================================\n\n");
Console console = System.console();
String user = console.readLine("Username: ");
String password = new String((console.readPassword("Password: ")));
String testID = console.readLine("TestID: ");
String pattern = "[Tt]?(\\d+)";
Pattern pat = Pattern.compile(pattern);
Matcher m = pat.matcher(testID);
if (!m.matches()) {
System.out.println("Test ID doesn't have a valid format");
return;
}
testID = m.group(1);
String csvFilePath = console.readLine("CSV Test File: ");
if (????.equals("app2")) {
TestApi2 test = new TestApi2(user, password, testID, csvFilePath);
test.runTest();
} else if (????.equals("app3")) {
TestApi3 test2 = new TestApi3(user, password, testID, csvFilePath);
test2.runTest();
} else {
System.out.println("Unknown");
}
} else if (args.length == 1 && args[0].equalsIgnoreCase("--help")) {
}
}
}