Add Split and Merge functions to CLI

Create CanellableRunnable abstraction
Change Split/Merge implementation to CanellableRunnable
This commit is contained in:
Dmitry Isaenko 2020-07-13 00:16:44 +03:00
parent 3592c9086b
commit dbb7c8e2e1
19 changed files with 358 additions and 65 deletions

View file

@ -78,6 +78,17 @@ public class CommandLineInterface {
return;
}
*/
if (cli.hasOption("s") || cli.hasOption("split")){
final String[] arguments = cli.getOptionValues("split");
new Split(arguments);
return;
}
if (cli.hasOption("m") || cli.hasOption("merge")){
final String[] arguments = cli.getOptionValues("merge");
new Merge(arguments);
return;
}
}
catch (ParseException pe){
System.out.println(pe.getLocalizedMessage() +
@ -138,7 +149,7 @@ public class CommandLineInterface {
.longOpt("tinfoil")
.desc("Install via Tinfoil/Awoo USB mode.")
.hasArgs()
.argName("FILE1 ...")
.argName("FILE...")
.build();
/* GoldLeaf USB */
final Option glOption = Option.builder("g")
@ -156,6 +167,18 @@ public class CommandLineInterface {
.argName("DIRECTORY")
.build();
*/
final Option splitOption = Option.builder("s")
.longOpt("split")
.desc("Split files. Check '-s help' for information.")
.hasArgs()
.argName("...")
.build();
final Option mergeOption = Option.builder("m")
.longOpt("merge")
.desc("Merge files. Check '-m help' for information.")
.hasArgs()
.argName("...")
.build();
final OptionGroup group = new OptionGroup();
group.addOption(rcmOption);
@ -166,6 +189,8 @@ public class CommandLineInterface {
group.addOption(tinfoilOption);
group.addOption(glOption);
//group.addOption(nxdtOption);
group.addOption(splitOption);
group.addOption(mergeOption);
options.addOptionGroup(group);

View file

@ -0,0 +1,108 @@
/*
Copyright 2019-2020 Dmitry Isaenko
This file is part of NS-USBloader.
NS-USBloader is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NS-USBloader is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
*/
package nsusbloader.cli;
import nsusbloader.Utilities.splitmerge.MergeTask;
import nsusbloader.Utilities.splitmerge.SplitTask;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Merge {
private String[] arguments;
private String saveTo;
private String[] splitFiles;
Merge(String[] arguments) throws InterruptedException, IncorrectSetupException{
this.arguments = arguments;
checkArguments();
parseArguments();
printFilesForMerge();
runBackend();
}
private void checkArguments() throws IncorrectSetupException{
if (arguments == null || arguments.length == 0) {
throw new IncorrectSetupException("No arguments.\n" +
"Try 'ns-usbloader -m help' for more information.");
}
if (arguments.length == 1){
if (isHelpDirective(arguments[0])){
showHelp();
return;
}
throw new IncorrectSetupException("Not enough arguments.\n"
+ "Try 'ns-usbloader -m help' for more information.");
}
this.saveTo = arguments[0];
File saveToFile = new File(saveTo);
if (! saveToFile.exists() || saveToFile.isFile()){
throw new IncorrectSetupException("First argument must be existing directory.");
}
}
private boolean isHelpDirective(String argument){
return argument.equals("help");
}
private void showHelp() throws IncorrectSetupException{
throw new IncorrectSetupException("Usage:\n"
+ "\tns-usbloader -m <SAVE_TO_DIR> <SPLIT_FILE>...\n"
+ "\n\nOptions:"
+ "\n\tSAVE_TO_DIR\tWhere results should be saved"
+ "\n\tSPLIT_FILE\tOne or more split-files (folders) to merge");
}
private void parseArguments() throws IncorrectSetupException{
List<String> files = new ArrayList<>();
for (int i = 1; i < arguments.length; i++){
File file = new File(arguments[i]);
if (file.isDirectory())
files.add(file.getAbsolutePath());
}
if (files.isEmpty()){
throw new IncorrectSetupException("No files specified.\n" +
"Try 'ns-usbloader -m help' for more information.");
}
this.splitFiles = files.toArray(new String[0]);
}
private void printFilesForMerge(){
System.out.println("Next files will be merged:");
for (String f : this.splitFiles)
System.out.println(" "+f);
}
private void runBackend() throws InterruptedException{
for (String filePath : splitFiles){
Runnable mergeTask = new MergeTask(filePath, saveTo);
Thread thread = new Thread(mergeTask);
thread.setDaemon(true);
thread.start();
thread.join();
}
}
}

View file

@ -0,0 +1,106 @@
/*
Copyright 2019-2020 Dmitry Isaenko
This file is part of NS-USBloader.
NS-USBloader is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NS-USBloader is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
*/
package nsusbloader.cli;
import nsusbloader.Utilities.splitmerge.SplitTask;
import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Split {
private String[] arguments;
private String saveTo;
private String[] files;
Split(String[] arguments) throws InterruptedException, IncorrectSetupException{
this.arguments = arguments;
checkArguments();
parseArguments();
printFilesForSplit();
runBackend();
}
private void checkArguments() throws IncorrectSetupException{
if (arguments == null || arguments.length == 0) {
throw new IncorrectSetupException("No arguments.\n" +
"Try 'ns-usbloader -s help' for more information.");
}
if (arguments.length == 1){
if (isHelpDirective(arguments[0])){
showHelp();
return;
}
throw new IncorrectSetupException("Not enough arguments.\n" +
"Try 'ns-usbloader -s help' for more information.");
}
this.saveTo = arguments[0];
File saveToFile = new File(saveTo);
if (! saveToFile.exists() || saveToFile.isFile()){
throw new IncorrectSetupException("First argument must be existing directory.");
}
}
private boolean isHelpDirective(String argument){
return argument.equals("help");
}
private void showHelp() throws IncorrectSetupException{
throw new IncorrectSetupException("Usage:\n"
+ "\tns-usbloader -s <SAVE_TO_DIR> <FILE>...\n"
+ "\n\nOptions:"
+ "\n\tSAVE_TO_DIR\tWhere results should be saved"
+ "\n\tFILE\t\tOne or more files to split");
}
private void parseArguments() throws IncorrectSetupException{
List<String> files = new ArrayList<>();
for (int i = 1; i < arguments.length; i++){
File file = new File(arguments[i]);
if (file.isFile())
files.add(file.getAbsolutePath());
}
if (files.isEmpty()){
throw new IncorrectSetupException("No files specified.\n" +
"Try 'ns-usbloader -s help' for more information.");
}
this.files = files.toArray(new String[0]);
}
private void printFilesForSplit(){
System.out.println("Next files will be splitted:");
for (String f : this.files)
System.out.println(" "+f);
}
private void runBackend() throws InterruptedException{
for (String filePath : files){
Runnable splitTaks = new SplitTask(filePath, saveTo);
Thread thread = new Thread(splitTaks);
thread.setDaemon(true);
thread.start();
thread.join();
}
}
}