Skip to content

Commit 3690c63

Browse files
committed
CLI support for Tinfoil/Awoo Net-install mode.
1 parent 3c89df9 commit 3690c63

File tree

3 files changed

+85
-62
lines changed

3 files changed

+85
-62
lines changed

src/main/java/nsusbloader/cli/CommandLineInterface.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public CommandLineInterface(String[] args) {
6666
System.out.println(pe.getLocalizedMessage() +
6767
"\nTry 'ns-usbloader --help' for more information.");
6868
}
69+
catch (IncorrectSetupException iee){
70+
System.out.println(iee.getLocalizedMessage());
71+
}
6972
catch (InterruptedException ignore){}
7073
catch (Exception e){
7174
System.out.println("CLI error");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package nsusbloader.cli;
2+
3+
public class IncorrectSetupException extends Exception {
4+
public IncorrectSetupException(String errorMessage){
5+
super(errorMessage);
6+
}
7+
}

src/main/java/nsusbloader/cli/TinfoilNet.java

Lines changed: 75 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
import java.util.List;
2626

2727
// TODO: Add 'don't serve requests' option
28-
// TODO: Refactor: remove duplicates; make logic flow more 'exception-driven'
2928
public class TinfoilNet {
3029

30+
private String[] arguments;
31+
3132
private String nsIp;
3233

3334
private String hostIp = "";
@@ -36,49 +37,40 @@ public class TinfoilNet {
3637

3738
private int parseFileSince = 1;
3839

39-
TinfoilNet(String[] arguments) throws InterruptedException{
40+
private List<File> filesList;
4041

41-
if (arguments == null) {
42-
showIncorrectCommandMessage();
43-
return;
42+
TinfoilNet(String[] arguments) throws InterruptedException, IncorrectSetupException{
43+
this.arguments = arguments;
44+
checkArguments();
45+
parseNsIP();
46+
parseHostIPAndExtras();
47+
parseFilesArguments();
48+
runTinfoilNetBackend();
49+
}
50+
51+
private void checkArguments() throws IncorrectSetupException{
52+
if (arguments == null || arguments.length == 0) {
53+
throw new IncorrectSetupException("No arguments.\n" +
54+
"Try 'ns-usbloader -n help' for more information.");
4455
}
4556

4657
if (arguments.length == 1){
47-
if (isHelp(arguments[0]))
58+
if (isHelpDirective(arguments[0])){
4859
showHelp();
60+
return;
61+
}
4962
else
50-
showIncorrectCommandMessage();
51-
return;
63+
throw new IncorrectSetupException("Not enough arguments.\n" +
64+
"Try 'ns-usbloader -n help' for more information.");
5265
}
5366

54-
if (arguments.length < 2){
55-
showIncorrectCommandMessage();
56-
return;
67+
if (arguments.length == 2 && arguments[1].startsWith("hostip=")) {
68+
throw new IncorrectSetupException("Not enough arguments.\n" +
69+
"Try 'ns-usbloader -n help' for more information.");
5770
}
58-
59-
if (parseNsIP(arguments[0]))
60-
return;
61-
parseHostIPAndExtras(arguments[1]);
62-
if (checkArgumentsCount(arguments.length))
63-
return;
64-
65-
List<File> filesList = new ArrayList<>();
66-
for (; parseFileSince < arguments.length; parseFileSince++)
67-
filesList.add(new File(arguments[parseFileSince]));
68-
69-
NETCommunications netCommunications = new NETCommunications(
70-
filesList,
71-
nsIp,
72-
false,
73-
hostIp,
74-
hostPortNum,
75-
hostExtras);
76-
Thread netCommThread = new Thread(netCommunications);
77-
netCommThread.start();
78-
netCommThread.join();
7971
}
8072

81-
private boolean isHelp(String argument){
73+
private boolean isHelpDirective(String argument){
8274
return argument.equals("help");
8375
}
8476
private void showHelp(){
@@ -89,42 +81,63 @@ private void showHelp(){
8981
+ "\n\tnsip=<ip>\t\t\tDefine NS IP address (mandatory)"
9082
+ "\n\thostip=<ip[:port][/extra]>\tDefine this host IP address. Will be obtained automatically if not set.");
9183
}
92-
private void showIncorrectCommandMessage(){
93-
System.out.println("Try 'ns-usbloader -n help' for more information.");
84+
85+
private void parseNsIP() throws IncorrectSetupException{
86+
String argument1 = arguments[0];
87+
88+
if (! argument1.startsWith("nsip="))
89+
throw new IncorrectSetupException("First argument must be 'nsip=<ip_address>'\n" +
90+
"Try 'ns-usbloader -n help' for more information.");
91+
92+
nsIp = argument1.replaceAll("^nsip=", "");
93+
94+
if (nsIp.isEmpty())
95+
throw new IncorrectSetupException("No spaces allowed before or after 'nsip=<ip_address>' argument.\n" +
96+
"Try 'ns-usbloader -n help' for more information.");
9497
}
9598

96-
private boolean parseNsIP(String argument1){
97-
if (argument1.startsWith("nsip=")){
98-
nsIp = argument1.replaceAll("^nsip=", "");
99+
private void parseHostIPAndExtras(){
100+
String argument2 = arguments[1];
99101

100-
if (nsIp.isEmpty()) {
101-
showIncorrectCommandMessage();
102-
return true;
103-
}
104-
}
105-
else{
106-
showIncorrectCommandMessage();
107-
return true;
108-
}
109-
return false;
102+
if (! argument2.startsWith("hostip="))
103+
return;
104+
105+
parseFileSince = 2;
106+
hostIp = argument2.replaceAll("(^hostip=)|(:.+?$)|(:$)", "");
107+
108+
if (argument2.contains(":"))
109+
hostPortNum = argument2.replaceAll("(^.+:)|(/.+?$)|(/$)", "");
110+
111+
if (argument2.contains("/"))
112+
hostExtras = argument2.replaceAll("^[^/]*/", "");
110113
}
111-
private void parseHostIPAndExtras(String argument2){
112-
if (argument2.startsWith("hostip=")){
113-
parseFileSince = 2;
114-
hostIp = argument2.replaceAll("(^hostip=)|(:.+?$)|(:$)", "");
115114

116-
if (argument2.contains(":"))
117-
hostPortNum = argument2.replaceAll("(^.+:)|(/.+?$)|(/$)", "");
115+
private void parseFilesArguments() throws IncorrectSetupException{
116+
filesList = new ArrayList<>();
117+
File file;
118118

119-
if (argument2.contains("/"))
120-
hostExtras = argument2.replaceAll("^[^/]*/", "");
119+
for (; parseFileSince < arguments.length; parseFileSince++) {
120+
file = new File(arguments[parseFileSince]);
121+
if (file.exists())
122+
filesList.add(file);
121123
}
122-
}
123-
private boolean checkArgumentsCount(int argumentsLength){
124-
if (argumentsLength == parseFileSince){
125-
showIncorrectCommandMessage();
126-
return true;
124+
125+
if (filesList.size() == 0) {
126+
throw new IncorrectSetupException("File(s) doesn't exists.\n" +
127+
"Try 'ns-usbloader -n help' for more information.");
127128
}
128-
return false;
129+
}
130+
131+
private void runTinfoilNetBackend() throws InterruptedException{
132+
NETCommunications netCommunications = new NETCommunications(
133+
filesList,
134+
nsIp,
135+
false,
136+
hostIp,
137+
hostPortNum,
138+
hostExtras);
139+
Thread netCommThread = new Thread(netCommunications);
140+
netCommThread.start();
141+
netCommThread.join();
129142
}
130143
}

0 commit comments

Comments
 (0)