94 lines
3.3 KiB
Java
94 lines
3.3 KiB
Java
import java.util.Objects;
|
|
import java.lang.Exception;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.util.Scanner;
|
|
|
|
import java.net.URL;
|
|
import java.io.BufferedInputStream;
|
|
import java.io.FileOutputStream;
|
|
|
|
public class Main {
|
|
public static void main(String[] args) {
|
|
boolean help_enqueued = false;
|
|
boolean allow_networking = true;
|
|
int verbosity = 1;
|
|
|
|
int p = 0;
|
|
while (p < args.length) {
|
|
try {
|
|
if (Objects.equals(args[p], "-h")) {
|
|
help_enqueued = true;
|
|
p += 1;
|
|
}
|
|
else if (Objects.equals(args[p], "-v")) {
|
|
verbosity = Integer.parseInt(args[p+1]);
|
|
p += 2;
|
|
}
|
|
else if (Objects.equals(args[p], "--offline")) {
|
|
allow_networking = false;
|
|
p += 2;
|
|
}
|
|
else {
|
|
System.err.println("[ERROR] Unknown argument found: " + args[p] + "\n[ERROR] Please, use 'java Main -h' for help");
|
|
System.exit(1);
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
System.err.println("[ERROR] Exception while parsing CLI arguments: " + e);
|
|
System.err.println("[ERROR] Aborting further execution.");
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
if (help_enqueued) {
|
|
printHelp(allow_networking);
|
|
System.exit(0);
|
|
}
|
|
}
|
|
|
|
private static void fetchResource(String remote_url, String output_filename) {
|
|
try {
|
|
BufferedInputStream in = new BufferedInputStream(new URL(remote_url).openStream());
|
|
FileOutputStream fileOutputStream = new FileOutputStream(output_filename);
|
|
|
|
byte dataBuffer[] = new byte[1024];
|
|
int bytesRead;
|
|
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
|
|
fileOutputStream.write(dataBuffer, 0, bytesRead);
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println("[ERROR] Failed to fetch resource " + output_filename + "from " + remote_url + " due to the following exception: " + e);
|
|
}
|
|
}
|
|
|
|
private static void printHelp(boolean allow_net) {
|
|
try {
|
|
File help_file = new File("src/help.txt");
|
|
|
|
if (!help_file.exists()) {
|
|
System.err.println("[WARN] Help file is missing.");
|
|
if (allow_net) {
|
|
System.err.println("[INFO] Trying to recover it from the git server");
|
|
fetchResource("http://139.162.162.130:3000/dymik739/oop-labs-collection/raw/branch/lab2-dev/labs/2/src/help.txt", "src/help.txt");
|
|
} else {
|
|
System.err.println("[INFO] Networking is disabled, not recovering");
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
Scanner help_file_scanner = new Scanner(help_file);
|
|
|
|
while (help_file_scanner.hasNextLine()) {
|
|
System.out.print(help_file_scanner.nextLine());
|
|
}
|
|
|
|
help_file_scanner.close();
|
|
} catch (Exception e) {
|
|
System.out.println("[ERROR] Failed to read help due to the following exception: " + e);
|
|
System.exit(1);
|
|
}
|
|
}
|
|
}
|