Skip to content

Commit

Permalink
Single factory method for CommandWrapper in LocalGcdHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard authored and Ajay Kannan committed Sep 22, 2015
1 parent 0aecc40 commit a93da9e
Showing 1 changed file with 16 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,8 @@ private static Path installedGcdPath() {
}

private static String installedGcdVersion() throws IOException, InterruptedException {
CommandWrapper gcloudCommand;
if (isWindows()) {
gcloudCommand = CommandWrapper.cmd();
} else {
gcloudCommand = CommandWrapper.bash();
}
Process process = gcloudCommand.command("gcloud", "version").redirectErrorStream().start();
Process process =
CommandWrapper.create().command("gcloud", "version").redirectErrorStream().start();
process.waitFor();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()))) {
Expand Down Expand Up @@ -225,6 +220,14 @@ private static class CommandWrapper {

private CommandWrapper() {
this.prefix = new ArrayList<>();
if (isWindows()) {
this.prefix.add("cmd");
this.prefix.add("/C");
this.nullFilename = "NUL:";
} else {
this.prefix.add("bash");
this.nullFilename = "/dev/null";
}
}

public CommandWrapper command(String... command) {
Expand Down Expand Up @@ -275,19 +278,8 @@ public Process start() throws IOException {
return builder().start();
}

public static CommandWrapper cmd() {
CommandWrapper wrapper = new CommandWrapper();
wrapper.prefix.add("cmd");
wrapper.prefix.add("/C");
wrapper.nullFilename = "NUL:";
return wrapper;
}

public static CommandWrapper bash() {
CommandWrapper wrapper = new CommandWrapper();
wrapper.prefix.add("bash");
wrapper.nullFilename = "/dev/null";
return wrapper;
public static CommandWrapper create() {
return new CommandWrapper();
}
}

Expand Down Expand Up @@ -362,17 +354,11 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
File datasetFolder = new File(gcdPath.toFile(), projectId);
deleteRecurse(datasetFolder.toPath());

// create command wrappers
CommandWrapper gcdCreateCommand;
CommandWrapper gcdStartCommand;
// Get path to cmd executable
Path gcdAbsolutePath;
if (isWindows()) {
gcdCreateCommand = CommandWrapper.cmd();
gcdStartCommand = CommandWrapper.cmd();
gcdAbsolutePath = executablePath.toAbsolutePath().resolve("gcd.cmd");
} else {
gcdCreateCommand = CommandWrapper.bash();
gcdStartCommand = CommandWrapper.bash();
gcdAbsolutePath = executablePath.toAbsolutePath().resolve("gcd.sh");
}

Expand All @@ -381,7 +367,8 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
log.log(Level.FINE, "Creating datastore for the project: {0}", projectId);
}
Process createProcess =
gcdCreateCommand.command(gcdAbsolutePath.toString(), "create", "-p", projectId, projectId)
CommandWrapper.create()
.command(gcdAbsolutePath.toString(), "create", "-p", projectId, projectId)
.redirectErrorInherit()
.directory(gcdPath)
.redirectOutputToNull()
Expand All @@ -393,7 +380,7 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
log.log(Level.FINE, "Starting datastore emulator for the project: {0}", projectId);
}
Process startProcess =
gcdStartCommand
CommandWrapper.create()
.command(gcdAbsolutePath.toString(), "start", "--testing", "--allow_remote_shutdown",
projectId)
.directory(gcdPath)
Expand Down

0 comments on commit a93da9e

Please sign in to comment.