Skip to content

Commit

Permalink
Merge pull request #36 from Anurag-Bharati/master
Browse files Browse the repository at this point in the history
Added support for command line argument.
  • Loading branch information
SaptarshiSarkar12 authored Oct 2, 2022
2 parents 728a735 + 15e5837 commit 8410470
Showing 1 changed file with 73 additions and 11 deletions.
84 changes: 73 additions & 11 deletions src/Drifty_CLI.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Scanner;

public class Drifty_CLI {
Expand All @@ -10,6 +13,7 @@ public class Drifty_CLI {
public static final String ANSI_PURPLE = "\u001B[35m";
private static boolean flag = false;
private static String fName = null;

public static void main(String[] args) {
logger.log("INFO", "Application Started !");
if (!flag) {
Expand All @@ -23,6 +27,42 @@ public static void main(String[] args) {
System.out.println(ANSI_PURPLE+"===================================================================="+ANSI_RESET);
}
flag = true;
if (args.length > 0){
String URL = args[0];
String name = null;
String location = null;
for (int i = 0; i<args.length;i++){
if (Objects.equals(args[i], "-help") || Objects.equals(args[i], "-h")){
help();
System.exit(0);
} else if (Objects.equals(args[i], "-name") || (Objects.equals(args[i], "-n"))){
name = args[i+1];
} else if (Objects.equals(args[i], "-location") || (Objects.equals(args[i], "-l"))){
location = args[i+1];
}
}
if (!validURL(URL)){
System.exit(0);
}
containsFile(URL);
fName = name==null? fName: name;
if (fName==null){
System.out.print("Enter the filename (with file extension) : ");
fName = SC.nextLine();
}
downloadsFolder = location;
if (downloadsFolder == null){
saveToDefault();
} else{
downloadsFolder = downloadsFolder.replace('/', '\\');
if (!(downloadsFolder.endsWith("\\"))) {
downloadsFolder = downloadsFolder + System.getProperty("file.separator");
}
}
FileDownloader fDownload = new FileDownloader(URL, fName, downloadsFolder);
fDownload.run();
System.exit(0);
}
while(true) {
fName = null;
System.out.print("Enter the link to the file : ");
Expand Down Expand Up @@ -66,17 +106,7 @@ public static void main(String[] args) {
System.out.print("Do you want to download the file in your default downloads folder? (Enter Y for yes and N for no) : ");
char default_folder = SC.nextLine().toLowerCase().charAt(0);
if (default_folder == 'y') {
System.out.println("Trying to auto-detect default Downloads folder...");
logger.log("INFO", "Trying to auto-detect default Downloads folder...");
downloadsFolder = DefaultDownloadFolderLocationFinder.findPath() + System.getProperty("file.separator");
if (downloadsFolder.equals(System.getProperty("file.separator"))) {
System.out.println("Failed to retrieve default download folder!");
logger.log("ERROR", "Failed to retrieve default download folder!");
enterDownloadsFolder();
} else {
System.out.println("Default download folder detected : " + downloadsFolder);
logger.log("INFO", "Default download folder detected : " + downloadsFolder);
}
saveToDefault();
} else if (default_folder == 'n') {
enterDownloadsFolder();
} else {
Expand Down Expand Up @@ -104,6 +134,20 @@ private static void enterDownloadsFolder(){
}
}

private static void saveToDefault(){
System.out.println("Trying to auto-detect default Downloads folder...");
logger.log("INFO", "Trying to auto-detect default Downloads folder...");
downloadsFolder = DefaultDownloadFolderLocationFinder.findPath() + System.getProperty("file.separator");
if (downloadsFolder.equals(System.getProperty("file.separator"))) {
System.out.println("Failed to retrieve default download folder!");
logger.log("ERROR", "Failed to retrieve default download folder!");
enterDownloadsFolder();
} else {
System.out.println("Default download folder detected : " + downloadsFolder);
logger.log("INFO", "Default download folder detected : " + downloadsFolder);
}
}

private static boolean validURL(String link){
try{
new URL(link).toURI();
Expand Down Expand Up @@ -134,4 +178,22 @@ private static boolean containsFile(String link){
System.out.println("Filename detected : " + fName);
return true;
}

private static String getCurrentTimeAsName(){
return new SimpleDateFormat("yyyy-MM-dd-HHmmss").format(new Date());
}

private static void help(){
System.out.println(ANSI_RESET+"\n\033[38;31;48;40;1m--=| DRIFTY CLI HELP |=--"+ANSI_RESET);
System.out.println("\033[38;31;48;40;0m v 1.1.0"+ANSI_RESET);
System.out.println("For more information visit: https://github.com/SaptarshiSarkar12/Drifty/");
System.out.println("\033[31;1mRequired parameter: File URL"+ANSI_RESET+" \033[3m(This must be the first arg. you pass)"+ANSI_RESET);
System.out.println("\033[33;1mOptional parameters:");
System.out.println("\033[97;1mName ShortForm Default Description"+ANSI_RESET);
System.out.println("-location -l Downloads The location on your computer where content downloaded from Drifty are placed.");
System.out.println("-name -n Source Renames file.");
System.out.println("-help -h N/A Provides concise information for Drifty CLI.\n");
System.out.println("\033[97;1mExample:" + ANSI_RESET + " \n> \033[37;1mjava Drifty_CLI https://example.com/object.png -n obj.png -l C:/Users/example"+ ANSI_RESET );
System.out.println("\033[37;3m* Requires java 18 or higher. \n"+ANSI_RESET);
}
}

0 comments on commit 8410470

Please sign in to comment.