Changes behavior to only allow the downloaded file to be optionally executed, not any chosen file

This commit is contained in:
Kristian Knarvik 2020-08-29 12:14:56 +02:00
parent f304a7a546
commit 4cb0be6a8c

View File

@ -11,10 +11,10 @@ import java.util.concurrent.TimeUnit;
public class Main {
private static final String usageString = "Usage: downloadURL [sourceProgram] [targetFile] [delay]\nThe source " +
"program will be executed upon successful download. The default target will be the file name defined in " +
"the download URL. The default delay is 5 seconds, and is the time to wait for the calling .jar file to " +
"be writable.";
private static final String usageString = "Usage: downloadURL [runAfterDownload (y/n)] [targetFile] [delay]\nThe " +
"downloaded file will be run if runAfterDownload is given as y/yes. The default target will be the file " +
"name defined in the download URL. The default delay is 5 seconds, and is the time to wait for the " +
"calling .jar file to be writable.";
private static final String invalidTargetFilename = "Target filename is invalid. Please set a targetFile argument" +
" ending with a three-letter extension";
private static final String invalidDownloadURL = "Invalid download URL. Only URLs pointing to knarcraft.net can " +
@ -22,16 +22,20 @@ public class Main {
private static String downloadURL;
private static String targetFile;
private static String sourceProgram;
private static boolean runAfterUpdate = false;
private static int delay = 5;
public static void main(String[] args) throws InterruptedException, IOException {
parseAndValidateArguments(args);
TimeUnit.SECONDS.sleep(delay);
//Try to download the updated file
boolean success = DownloaderUtil.downloadFile(downloadURL,
Paths.get(DownloaderUtil.getApplicationWorkDirectory() + File.separator + targetFile));
if (success && sourceProgram != null && !sourceProgram.equals("")) {
DownloaderUtil.runExecutable(sourceProgram);
//Run the downloaded file to start the updated software
if (success && runAfterUpdate) {
DownloaderUtil.runExecutable(targetFile);
}
}
@ -42,7 +46,7 @@ public class Main {
*/
private static void parseAndValidateArguments(String[] args) {
if (args.length >= 2) {
sourceProgram = args[1];
runAfterUpdate = args[1].toLowerCase().startsWith("y");
}
if (args.length >= 4) {