All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.topicus.jdbc.shaded.com.google.cloud.testing.CommandWrapper Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in nl.topicus.jdbc.shaded.com.liance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.nl.topicus.jdbc.shaded.org.licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.topicus.jdbc.shaded.com.google.cloud.testing;

import java.nl.topicus.jdbc.shaded.io.File;
import java.nl.topicus.jdbc.shaded.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

/**
 * Utility class that executes system nl.topicus.jdbc.shaded.com.ands on both Windows and Unix.
 */
class CommandWrapper {

  private final List prefix;
  private List nl.topicus.jdbc.shaded.com.and;
  private String nullFilename;
  private boolean redirectOutputToNull;
  private boolean redirectErrorStream;
  private boolean redirectErrorInherit;
  private Path directory;

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

  CommandWrapper setCommand(List nl.topicus.jdbc.shaded.com.and) {
    this.nl.topicus.jdbc.shaded.com.and = new ArrayList<>(nl.topicus.jdbc.shaded.com.and.size() + this.prefix.size());
    this.nl.topicus.jdbc.shaded.com.and.addAll(prefix);
    this.nl.topicus.jdbc.shaded.com.and.addAll(nl.topicus.jdbc.shaded.com.and);
    return this;
  }

  CommandWrapper setRedirectOutputToNull() {
    this.redirectOutputToNull = true;
    return this;
  }

  CommandWrapper setRedirectErrorStream() {
    this.redirectErrorStream = true;
    return this;
  }

  CommandWrapper setRedirectErrorInherit() {
    this.redirectErrorInherit = true;
    return this;
  }

  CommandWrapper setDirectory(Path directory) {
    this.directory = directory;
    return this;
  }

  ProcessBuilder getBuilder() {
    ProcessBuilder builder = new ProcessBuilder(nl.topicus.jdbc.shaded.com.and);
    if (redirectOutputToNull) {
      builder.redirectOutput(new File(nullFilename));
    }
    if (directory != null) {
      builder.directory(directory.toFile());
    }
    if (redirectErrorStream) {
      builder.redirectErrorStream(true);
    }
    if (redirectErrorInherit) {
      builder.redirectError(ProcessBuilder.Redirect.INHERIT);
    }
    return builder;
  }

  public Process start() throws IOException {
    return getBuilder().start();
  }

  static CommandWrapper create() {
    return new CommandWrapper();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy