|
50 | 50 | import java.security.NoSuchAlgorithmException; |
51 | 51 | import java.util.ArrayList; |
52 | 52 | import java.util.Arrays; |
| 53 | +import java.util.HashMap; |
53 | 54 | import java.util.List; |
54 | 55 | import java.util.Locale; |
| 56 | +import java.util.Map; |
55 | 57 | import java.util.regex.Pattern; |
56 | 58 | import java.util.zip.ZipEntry; |
57 | 59 | import java.util.zip.ZipInputStream; |
@@ -478,39 +480,65 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO |
478 | 480 | }); |
479 | 481 | } |
480 | 482 |
|
481 | | - public static LocalGcdHelper start(String projectId, int port) throws IOException, InterruptedException { |
| 483 | + public static LocalGcdHelper start(String projectId, int port) |
| 484 | + throws IOException, InterruptedException { |
482 | 485 | LocalGcdHelper helper = new LocalGcdHelper(projectId, port); |
483 | 486 | helper.start(); |
484 | 487 | return helper; |
485 | 488 | } |
486 | 489 |
|
487 | 490 | public static void main(String... args) throws IOException, InterruptedException { |
488 | | - if (args.length == 1) { |
489 | | - switch (args[0]) { |
490 | | - case "START": |
491 | | - if (!isActive(DEFAULT_PROJECT_ID, findAvailablePort(DEFAULT_PORT))) { |
492 | | - LocalGcdHelper helper = start(DEFAULT_PROJECT_ID, findAvailablePort(DEFAULT_PORT)); |
493 | | - try (FileWriter writer = new FileWriter(".local_gcd_helper")) { |
494 | | - writer.write(helper.gcdPath.toAbsolutePath().toString()); |
495 | | - } |
| 491 | + Map<String, String> parsedArgs = parseArgs(args); |
| 492 | + String action = parsedArgs.get("action"); |
| 493 | + int port = (parsedArgs.get("port") == null) ? DEFAULT_PORT |
| 494 | + : Integer.parseInt(parsedArgs.get("port")); |
| 495 | + switch (action) { |
| 496 | + case "START": |
| 497 | + if (!isActive(DEFAULT_PROJECT_ID, port)) { |
| 498 | + LocalGcdHelper helper = start(DEFAULT_PROJECT_ID, port); |
| 499 | + try (FileWriter writer = new FileWriter(".local_gcd_helper")) { |
| 500 | + writer.write( |
| 501 | + helper.gcdPath.toAbsolutePath().toString() + System.lineSeparator()); |
| 502 | + writer.write(Integer.toString(port)); |
496 | 503 | } |
497 | | - return; |
498 | | - case "STOP": |
499 | | - sendQuitRequest(DEFAULT_PORT); |
500 | | - File file = new File(".local_gcd_helper"); |
501 | | - if (file.exists()) { |
502 | | - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { |
503 | | - String path = reader.readLine(); |
504 | | - deleteRecurse(Paths.get(path)); |
505 | | - } |
506 | | - file.delete(); |
| 504 | + } |
| 505 | + return; |
| 506 | + case "STOP": |
| 507 | + File file = new File(".local_gcd_helper"); |
| 508 | + String path = null; |
| 509 | + boolean fileExists = file.exists(); |
| 510 | + if (fileExists) { |
| 511 | + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { |
| 512 | + path = reader.readLine(); |
| 513 | + port = Integer.parseInt(reader.readLine()); |
507 | 514 | } |
508 | | - return; |
509 | | - default: |
510 | | - break; |
| 515 | + } |
| 516 | + sendQuitRequest(port); |
| 517 | + if (fileExists) { |
| 518 | + deleteRecurse(Paths.get(path)); |
| 519 | + file.delete(); |
| 520 | + } |
| 521 | + return; |
| 522 | + default: |
| 523 | + break; |
| 524 | + } |
| 525 | + } |
| 526 | + |
| 527 | + private static Map<String, String> parseArgs(String[] args) { |
| 528 | + Map<String, String> parsedArgs = new HashMap<String, String>(); |
| 529 | + for (String arg : args) { |
| 530 | + if (arg.startsWith("--port=")) { |
| 531 | + parsedArgs.put("port", arg.substring("--port=".length())); |
| 532 | + } else if (arg.equals("START") || arg.equals("STOP")) { |
| 533 | + parsedArgs.put("action", arg); |
| 534 | + } else { |
| 535 | + throw new RuntimeException("Only accepts START, STOP, and --port=<port #> as arguments"); |
511 | 536 | } |
512 | 537 | } |
513 | | - throw new RuntimeException("expecting only START | STOP"); |
| 538 | + if (parsedArgs.get("action") == null) { |
| 539 | + throw new RuntimeException("EXPECTING START | STOP"); |
| 540 | + } |
| 541 | + return parsedArgs; |
514 | 542 | } |
515 | 543 |
|
516 | 544 | public static boolean isActive(String projectId, int port) { |
|
0 commit comments