CLI support for Tinfoil/Awoo Net-install mode.

This commit is contained in:
Dmitry Isaenko 2020-07-06 13:35:45 +03:00
parent 3c89df9dcd
commit 3690c63399
3 changed files with 92 additions and 69 deletions

View file

@ -66,6 +66,9 @@ public class CommandLineInterface {
System.out.println(pe.getLocalizedMessage() + System.out.println(pe.getLocalizedMessage() +
"\nTry 'ns-usbloader --help' for more information."); "\nTry 'ns-usbloader --help' for more information.");
} }
catch (IncorrectSetupException iee){
System.out.println(iee.getLocalizedMessage());
}
catch (InterruptedException ignore){} catch (InterruptedException ignore){}
catch (Exception e){ catch (Exception e){
System.out.println("CLI error"); System.out.println("CLI error");

View file

@ -0,0 +1,7 @@
package nsusbloader.cli;
public class IncorrectSetupException extends Exception {
public IncorrectSetupException(String errorMessage){
super(errorMessage);
}
}

View file

@ -25,9 +25,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
// TODO: Add 'don't serve requests' option // TODO: Add 'don't serve requests' option
// TODO: Refactor: remove duplicates; make logic flow more 'exception-driven'
public class TinfoilNet { public class TinfoilNet {
private String[] arguments;
private String nsIp; private String nsIp;
private String hostIp = ""; private String hostIp = "";
@ -36,36 +37,98 @@ public class TinfoilNet {
private int parseFileSince = 1; private int parseFileSince = 1;
TinfoilNet(String[] arguments) throws InterruptedException{ private List<File> filesList;
if (arguments == null) { TinfoilNet(String[] arguments) throws InterruptedException, IncorrectSetupException{
showIncorrectCommandMessage(); this.arguments = arguments;
return; checkArguments();
parseNsIP();
parseHostIPAndExtras();
parseFilesArguments();
runTinfoilNetBackend();
}
private void checkArguments() throws IncorrectSetupException{
if (arguments == null || arguments.length == 0) {
throw new IncorrectSetupException("No arguments.\n" +
"Try 'ns-usbloader -n help' for more information.");
} }
if (arguments.length == 1){ if (arguments.length == 1){
if (isHelp(arguments[0])) if (isHelpDirective(arguments[0])){
showHelp(); showHelp();
return;
}
else else
showIncorrectCommandMessage(); throw new IncorrectSetupException("Not enough arguments.\n" +
return; "Try 'ns-usbloader -n help' for more information.");
} }
if (arguments.length < 2){ if (arguments.length == 2 && arguments[1].startsWith("hostip=")) {
showIncorrectCommandMessage(); throw new IncorrectSetupException("Not enough arguments.\n" +
"Try 'ns-usbloader -n help' for more information.");
}
}
private boolean isHelpDirective(String argument){
return argument.equals("help");
}
private void showHelp(){
System.out.println("Usage:\n"
+ "\tns-usbloader -n nsip=<arg1> [hostip=<arg2>] FILE1 ...\n"
+ "\tns-usbloader --tfn nsip=<arg1> [hostip=<arg2>] FILE1 ..."
+ "\n\nOptions:"
+ "\n\tnsip=<ip>\t\t\tDefine NS IP address (mandatory)"
+ "\n\thostip=<ip[:port][/extra]>\tDefine this host IP address. Will be obtained automatically if not set.");
}
private void parseNsIP() throws IncorrectSetupException{
String argument1 = arguments[0];
if (! argument1.startsWith("nsip="))
throw new IncorrectSetupException("First argument must be 'nsip=<ip_address>'\n" +
"Try 'ns-usbloader -n help' for more information.");
nsIp = argument1.replaceAll("^nsip=", "");
if (nsIp.isEmpty())
throw new IncorrectSetupException("No spaces allowed before or after 'nsip=<ip_address>' argument.\n" +
"Try 'ns-usbloader -n help' for more information.");
}
private void parseHostIPAndExtras(){
String argument2 = arguments[1];
if (! argument2.startsWith("hostip="))
return; return;
parseFileSince = 2;
hostIp = argument2.replaceAll("(^hostip=)|(:.+?$)|(:$)", "");
if (argument2.contains(":"))
hostPortNum = argument2.replaceAll("(^.+:)|(/.+?$)|(/$)", "");
if (argument2.contains("/"))
hostExtras = argument2.replaceAll("^[^/]*/", "");
}
private void parseFilesArguments() throws IncorrectSetupException{
filesList = new ArrayList<>();
File file;
for (; parseFileSince < arguments.length; parseFileSince++) {
file = new File(arguments[parseFileSince]);
if (file.exists())
filesList.add(file);
} }
if (parseNsIP(arguments[0])) if (filesList.size() == 0) {
return; throw new IncorrectSetupException("File(s) doesn't exists.\n" +
parseHostIPAndExtras(arguments[1]); "Try 'ns-usbloader -n help' for more information.");
if (checkArgumentsCount(arguments.length)) }
return; }
List<File> filesList = new ArrayList<>();
for (; parseFileSince < arguments.length; parseFileSince++)
filesList.add(new File(arguments[parseFileSince]));
private void runTinfoilNetBackend() throws InterruptedException{
NETCommunications netCommunications = new NETCommunications( NETCommunications netCommunications = new NETCommunications(
filesList, filesList,
nsIp, nsIp,
@ -77,54 +140,4 @@ public class TinfoilNet {
netCommThread.start(); netCommThread.start();
netCommThread.join(); netCommThread.join();
} }
private boolean isHelp(String argument){
return argument.equals("help");
}
private void showHelp(){
System.out.println("Usage:\n"
+ "\tns-usbloader -n nsip=<arg1> [hostip=<arg2>] FILE1 ...\n"
+ "\tns-usbloader --tfn nsip=<arg1> [hostip=<arg2>] FILE1 ..."
+ "\n\nOptions:"
+ "\n\tnsip=<ip>\t\t\tDefine NS IP address (mandatory)"
+ "\n\thostip=<ip[:port][/extra]>\tDefine this host IP address. Will be obtained automatically if not set.");
}
private void showIncorrectCommandMessage(){
System.out.println("Try 'ns-usbloader -n help' for more information.");
}
private boolean parseNsIP(String argument1){
if (argument1.startsWith("nsip=")){
nsIp = argument1.replaceAll("^nsip=", "");
if (nsIp.isEmpty()) {
showIncorrectCommandMessage();
return true;
}
}
else{
showIncorrectCommandMessage();
return true;
}
return false;
}
private void parseHostIPAndExtras(String argument2){
if (argument2.startsWith("hostip=")){
parseFileSince = 2;
hostIp = argument2.replaceAll("(^hostip=)|(:.+?$)|(:$)", "");
if (argument2.contains(":"))
hostPortNum = argument2.replaceAll("(^.+:)|(/.+?$)|(/$)", "");
if (argument2.contains("/"))
hostExtras = argument2.replaceAll("^[^/]*/", "");
}
}
private boolean checkArgumentsCount(int argumentsLength){
if (argumentsLength == parseFileSince){
showIncorrectCommandMessage();
return true;
}
return false;
}
} }