CLI support for Tinfoil/Awoo Net-install mode.
This commit is contained in:
		
							parent
							
								
									3c89df9dcd
								
							
						
					
					
						commit
						3690c63399
					
				
					 3 changed files with 92 additions and 69 deletions
				
			
		|  | @ -66,6 +66,9 @@ public class CommandLineInterface { | |||
|             System.out.println(pe.getLocalizedMessage() + | ||||
|                     "\nTry 'ns-usbloader --help' for more information."); | ||||
|         } | ||||
|         catch (IncorrectSetupException iee){ | ||||
|             System.out.println(iee.getLocalizedMessage()); | ||||
|         } | ||||
|         catch (InterruptedException ignore){} | ||||
|         catch (Exception e){ | ||||
|             System.out.println("CLI error"); | ||||
|  |  | |||
|  | @ -0,0 +1,7 @@ | |||
| package nsusbloader.cli; | ||||
| 
 | ||||
| public class IncorrectSetupException extends Exception { | ||||
|     public IncorrectSetupException(String errorMessage){ | ||||
|         super(errorMessage); | ||||
|     } | ||||
| } | ||||
|  | @ -25,9 +25,10 @@ import java.util.ArrayList; | |||
| import java.util.List; | ||||
| 
 | ||||
| // TODO: Add 'don't serve requests' option | ||||
| // TODO: Refactor: remove duplicates; make logic flow more 'exception-driven' | ||||
| public class TinfoilNet { | ||||
| 
 | ||||
|     private String[] arguments; | ||||
| 
 | ||||
|     private String nsIp; | ||||
| 
 | ||||
|     private String hostIp = ""; | ||||
|  | @ -36,36 +37,98 @@ public class TinfoilNet { | |||
| 
 | ||||
|     private int parseFileSince = 1; | ||||
| 
 | ||||
|     TinfoilNet(String[] arguments) throws InterruptedException{ | ||||
|     private List<File> filesList; | ||||
| 
 | ||||
|         if (arguments == null) { | ||||
|             showIncorrectCommandMessage(); | ||||
|             return; | ||||
|     TinfoilNet(String[] arguments) throws InterruptedException, IncorrectSetupException{ | ||||
|         this.arguments = arguments; | ||||
|         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 (isHelp(arguments[0])) | ||||
|             if (isHelpDirective(arguments[0])){ | ||||
|                 showHelp(); | ||||
|                 return; | ||||
|             } | ||||
|             else | ||||
|                 showIncorrectCommandMessage(); | ||||
|             return; | ||||
|                 throw new IncorrectSetupException("Not enough arguments.\n" + | ||||
|                         "Try 'ns-usbloader -n help' for more information."); | ||||
|         } | ||||
| 
 | ||||
|         if (arguments.length < 2){ | ||||
|             showIncorrectCommandMessage(); | ||||
|         if (arguments.length == 2 && arguments[1].startsWith("hostip=")) { | ||||
|             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; | ||||
| 
 | ||||
|         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])) | ||||
|             return; | ||||
|         parseHostIPAndExtras(arguments[1]); | ||||
|         if (checkArgumentsCount(arguments.length)) | ||||
|             return; | ||||
| 
 | ||||
|         List<File> filesList = new ArrayList<>(); | ||||
|         for (; parseFileSince < arguments.length; parseFileSince++) | ||||
|             filesList.add(new File(arguments[parseFileSince])); | ||||
|         if (filesList.size() == 0) { | ||||
|             throw new IncorrectSetupException("File(s) doesn't exists.\n" + | ||||
|                     "Try 'ns-usbloader -n help' for more information."); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private void runTinfoilNetBackend() throws InterruptedException{ | ||||
|         NETCommunications netCommunications = new NETCommunications( | ||||
|                 filesList, | ||||
|                 nsIp, | ||||
|  | @ -77,54 +140,4 @@ public class TinfoilNet { | |||
|         netCommThread.start(); | ||||
|         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; | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Dmitry Isaenko
						Dmitry Isaenko