add development files for lab2
This commit is contained in:
		
							parent
							
								
									ded099dbac
								
							
						
					
					
						commit
						60c578b8bf
					
				
							
								
								
									
										89
									
								
								labs/2/Main.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								labs/2/Main.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,89 @@
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.util.Scanner;
 | 
			
		||||
import java.io.FileWriter;
 | 
			
		||||
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
import java.io.BufferedInputStream;
 | 
			
		||||
 | 
			
		||||
public class Main {
 | 
			
		||||
    private boolean disable_networking = false;
 | 
			
		||||
 | 
			
		||||
    public void main(String[] args) {
 | 
			
		||||
        boolean help_enqueued = false;
 | 
			
		||||
        int verbosity = 1;
 | 
			
		||||
        
 | 
			
		||||
        int p = 0;
 | 
			
		||||
        while (p < args.length) {
 | 
			
		||||
            try {
 | 
			
		||||
                if (Objects.equals(args[p], "-h")) {    
 | 
			
		||||
                    help_enqueued = true;
 | 
			
		||||
                }
 | 
			
		||||
                else if (Objects.equals(args[p], "-v")) {
 | 
			
		||||
                    verbosity = Integer.parseInt(args[p+1]);
 | 
			
		||||
                    p += 2;
 | 
			
		||||
                }
 | 
			
		||||
                else if (Objects.equals(args[p], "--offline")) {
 | 
			
		||||
                    disable_networking = true;
 | 
			
		||||
                    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();
 | 
			
		||||
            System.exit(0);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public fetchResource(String remote_url, String output_filename) {
 | 
			
		||||
        BufferedInputStream in = new BufferedInputStream(new URL(remote_url).openStream());
 | 
			
		||||
        FileOutputStream fileOutputStream = new FileOutputStream()
 | 
			
		||||
 | 
			
		||||
        byte dataBuffer[] = new byte[1024];
 | 
			
		||||
        int bytesRead;
 | 
			
		||||
        while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
 | 
			
		||||
            fileOutputStream.write(dataBuffer, 0, bytesRead);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public printHelp() {
 | 
			
		||||
        String help_text = "";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            File help_file = new File("src/help.txt");
 | 
			
		||||
            Scanner help_file_scanner = new Scanner(help_file);
 | 
			
		||||
 | 
			
		||||
        } catch (FileNotFoundException e) {
 | 
			
		||||
            System.err.println("[WARN] Help file missing.");
 | 
			
		||||
            if (!disable_networking) {
 | 
			
		||||
                System.err.println("[INFO] Trying to recover it from the git server");
 | 
			
		||||
                fetchResource("http://139.162.162.130:3000/", "src/help.txt");
 | 
			
		||||
                File help_file = new File("src/help.txt");
 | 
			
		||||
                Scanner help_file_scanner = new Scanner(help_file);
 | 
			
		||||
            } else {
 | 
			
		||||
                System.err.println("[INFO] --offline flag is set, not recovering");
 | 
			
		||||
                System.exit(1);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        while (help_file_scanner.hasNextLine()) {
 | 
			
		||||
            help_text += help_file_scanner.nextLine();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        help_file_scanner.close();
 | 
			
		||||
                    
 | 
			
		||||
        System.out.println(help_text);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								labs/2/src/help.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								labs/2/src/help.txt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
Dummy help file
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user