From e312702be4785479a962b0592820e05689a9e6e0 Mon Sep 17 00:00:00 2001 From: zhrexx <134622710+zhrexx@users.noreply.github.com> Date: Sun, 15 Sep 2024 17:18:07 +0300 Subject: [PATCH] Add files via upload --- src/main/java/com/zhrxxgroup/Main.java | 136 ++++++++++++------ src/main/java/com/zhrxxgroup/core/config.java | 12 ++ 2 files changed, 101 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/zhrxxgroup/Main.java b/src/main/java/com/zhrxxgroup/Main.java index cbda50e..80b1cd6 100644 --- a/src/main/java/com/zhrxxgroup/Main.java +++ b/src/main/java/com/zhrxxgroup/Main.java @@ -5,62 +5,106 @@ import com.zhrxxgroup.core.config; import com.zhrxxgroup.core.errors.ArgumentHandlingError; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class Main { - private static Downloader downloader = new Downloader(); + private static final Downloader downloader = new Downloader(); + private static final File configFile = new File(System.getProperty("user.home") + File.separator + config.PMN + File.separator + "c.txt"); + private static final String configDirPath = System.getProperty("user.home") + File.separator + config.PMN; + private static final File configDir = new File(configDirPath); public static void main(String[] args) { try { - if (args.length < 1) { - Scanner scanner = new Scanner(System.in); + setupConfig(); + handleUserInput(args); + } catch (ArgumentHandlingError | IOException e) { + System.err.println(config.prefix + "Error: " + e.getMessage()); + } + } - System.out.println(config.prefix + "Hey, follow the instructions"); - System.out.println("\n[delete | install]: "); - String action = scanner.nextLine().toLowerCase(); - handleAction(action, scanner); + private static void setupConfig() throws IOException { + if (!configDir.exists()) { + if (!configDir.mkdirs()) { + throw new IOException("Failed to create directory: " + configDirPath); + } + } + + if (!configFile.exists()) { + configFile.createNewFile(); + try (Scanner scanner = new Scanner(System.in); + FileWriter writer = new FileWriter(configFile)) { + + System.out.println(config.prefix + "Select a language"); + System.out.println("\n[english | russian]: "); + String lang = scanner.nextLine().trim().toLowerCase(); + if ("russian".equals(lang) || "ru".equals(lang)) { + writer.write("russian"); + } else { + writer.write("english"); + } + } + } + } + + private static void handleUserInput(String[] args) throws ArgumentHandlingError, IOException { + String language; + try (Scanner fileScanner = new Scanner(configFile)) { + if (fileScanner.hasNextLine()) { + language = fileScanner.nextLine().trim().toLowerCase(); } else { - String action = args[0].toLowerCase(); - handleAction(action, args); + throw new IOException("Config file is empty."); } - } catch (ArgumentHandlingError | IOException e) { - System.err.println(config.prefix + "Error: " + e.getMessage()); + } + + if ("russian".equals(language) || "english".equals(language)) { + if (args.length < 1) { + try (Scanner inputScanner = new Scanner(System.in)) { + promptForAction(inputScanner, language); + } + } else { + handleAction(args[0].toLowerCase(), args, language); + } + } else { + System.err.println(config.prefix + "Error: Unknown language in config file."); } } - private static void handleAction(String action, Scanner scanner) throws ArgumentHandlingError, IOException { + private static void promptForAction(Scanner scanner, String language) throws ArgumentHandlingError, IOException { + System.out.println(config.prefix + (language.equals("russian") ? config.ru_followInstructions_message : config.en_followInstructions_message)); + System.out.println("\n[delete | install]: "); + if (scanner.hasNextLine()) { + String action = scanner.nextLine().trim().toLowerCase(); + handleAction(action, scanner, language); + } else { + throw new ArgumentHandlingError("No input provided."); + } + } + + private static void handleAction(String action, Scanner scanner, String language) throws ArgumentHandlingError, IOException { switch (action) { case "install": - System.out.print("\nSearched Package Name: "); - String packageName = scanner.nextLine(); - - System.out.print("\nSearched Package Version: "); - double packageVersion = 0; - try { - packageVersion = Double.parseDouble(scanner.nextLine()); - } catch (NumberFormatException e) { - throw new ArgumentHandlingError("Invalid version number."); - } + System.out.print("\n" + (language.equals("russian") ? config.ru_searched_package_message : config.en_searched_package_message)); + String packageName = scanner.nextLine().trim(); + + System.out.print("\n" + (language.equals("russian") ? config.ru_searched_package_version : config.en_searched_package_version)); + double packageVersion = parseVersion(scanner.nextLine().trim()); - System.out.print("\nSearched Package Author: "); - String packageAuthor = scanner.nextLine(); + System.out.print("\n" + (language.equals("russian") ? config.ru_searched_package_author : config.en_searched_package_author)); + String packageAuthor = scanner.nextLine().trim(); downloader.download(packageName, packageVersion, packageAuthor); break; case "delete": - System.out.print("\nSearched Package Name: "); - String delPackageName = scanner.nextLine(); - - System.out.print("\nSearched Package Version: "); - double delPackageVersion = 0; - try { - delPackageVersion = Double.parseDouble(scanner.nextLine()); - } catch (NumberFormatException e) { - throw new ArgumentHandlingError("Invalid version number."); - } + System.out.print("\n" + (language.equals("russian") ? config.ru_searched_package_message : config.en_searched_package_message)); + String delPackageName = scanner.nextLine().trim(); + + System.out.print("\n" + (language.equals("russian") ? config.ru_searched_package_version : config.en_searched_package_version)); + double delPackageVersion = parseVersion(scanner.nextLine().trim()); new Packages().delete(delPackageName, delPackageVersion); break; @@ -70,19 +114,14 @@ private static void handleAction(String action, Scanner scanner) throws Argument } } - private static void handleAction(String action, String[] args) throws ArgumentHandlingError, IOException { + private static void handleAction(String action, String[] args, String language) throws ArgumentHandlingError, IOException { switch (action) { case "install": if (args.length < 4) { throw new ArgumentHandlingError("Insufficient arguments for install. Usage: install [author]"); } String packageName = args[1]; - double packageVersion; - try { - packageVersion = Double.parseDouble(args[2]); - } catch (NumberFormatException e) { - throw new ArgumentHandlingError("Invalid version number."); - } + double packageVersion = parseVersion(args[2]); String packageAuthor = args.length > 3 ? args[3] : "defaultAuthor"; downloader.download(packageName, packageVersion, packageAuthor); break; @@ -92,12 +131,7 @@ private static void handleAction(String action, String[] args) throws ArgumentHa throw new ArgumentHandlingError("Insufficient arguments for delete. Usage: delete "); } String delPackageName = args[1]; - double delPackageVersion; - try { - delPackageVersion = Double.parseDouble(args[2]); - } catch (NumberFormatException e) { - throw new ArgumentHandlingError("Invalid version number."); - } + double delPackageVersion = parseVersion(args[2]); new Packages().delete(delPackageName, delPackageVersion); break; @@ -105,4 +139,12 @@ private static void handleAction(String action, String[] args) throws ArgumentHa throw new ArgumentHandlingError("Unknown action: " + action); } } + + private static double parseVersion(String versionStr) throws ArgumentHandlingError { + try { + return Double.parseDouble(versionStr); + } catch (NumberFormatException e) { + throw new ArgumentHandlingError("Invalid version number."); + } + } } diff --git a/src/main/java/com/zhrxxgroup/core/config.java b/src/main/java/com/zhrxxgroup/core/config.java index bd4fc3a..c4c553d 100644 --- a/src/main/java/com/zhrxxgroup/core/config.java +++ b/src/main/java/com/zhrxxgroup/core/config.java @@ -5,4 +5,16 @@ public class config { public static String URL = "https://exemple.com/packages"; // Packages Download url (automatic will be added "URL + /--.zip" also it must be an POST) // path to store packages is ~//packages/ public static String prefix = "[TEST] "; // Log Prefix + + public static String + en_followInstructions_message = "Hey, follow the instructions", + ru_followInstructions_message = "Привет, следуй инструкциям", + + en_searched_package_message = "Searched Package Name: ", + en_searched_package_author = "Searched Package Author: ", + en_searched_package_version = "Searched Package Version: ", + + ru_searched_package_message = "Название искомого Пакета: ", + ru_searched_package_author = "Автор искомого Пакета: ", + ru_searched_package_version = "Версия искомого Пакета: "; }