Skip to content

Commit 00ec956

Browse files
author
Ajay Kannan
committed
Allow option to specify port in LocalGcdHelper's main
1 parent 22a9e99 commit 00ec956

1 file changed

Lines changed: 51 additions & 23 deletions

File tree

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
import java.security.NoSuchAlgorithmException;
5151
import java.util.ArrayList;
5252
import java.util.Arrays;
53+
import java.util.HashMap;
5354
import java.util.List;
5455
import java.util.Locale;
56+
import java.util.Map;
5557
import java.util.regex.Pattern;
5658
import java.util.zip.ZipEntry;
5759
import java.util.zip.ZipInputStream;
@@ -478,39 +480,65 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
478480
});
479481
}
480482

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 {
482485
LocalGcdHelper helper = new LocalGcdHelper(projectId, port);
483486
helper.start();
484487
return helper;
485488
}
486489

487490
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));
496503
}
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());
507514
}
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");
511536
}
512537
}
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;
514542
}
515543

516544
public static boolean isActive(String projectId, int port) {

0 commit comments

Comments
 (0)