Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
zhrexx authored Sep 15, 2024
1 parent 46db906 commit e312702
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 47 deletions.
136 changes: 89 additions & 47 deletions src/main/java/com/zhrxxgroup/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <package_name> <version> [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;
Expand All @@ -92,17 +131,20 @@ private static void handleAction(String action, String[] args) throws ArgumentHa
throw new ArgumentHandlingError("Insufficient arguments for delete. Usage: delete <package_name> <version>");
}
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;

default:
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.");
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/zhrxxgroup/core/config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@ public class config {
public static String URL = "https://exemple.com/packages"; // Packages Download url (automatic will be added "URL + /<author>-<package_name>-<version>.zip" also it must be an POST)
// path to store packages is ~/<PackageManagerName>/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 = "Версия искомого Пакета: ";
}

0 comments on commit e312702

Please sign in to comment.