/*
 * Copyright 2017 Spotify AB.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.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 com.spotify.spydra.api.process;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import jdk.nashorn.tools.Shell;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProcessHelper {
  private static final Logger LOGGER = LoggerFactory.getLogger(ProcessHelper.class);

  public static int executeCommand(List<String> command) throws IOException {
    LOGGER.debug("Executing command: " + StringUtils.join(command, " "));
    ProcessBuilder pb = new ProcessBuilder(command)
        .redirectError(ProcessBuilder.Redirect.INHERIT)
        .redirectOutput(ProcessBuilder.Redirect.INHERIT);

    Process p = pb.start();
    try {
      p.waitFor();
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      p.destroy();
    }

    return p.exitValue();
  }

  public static boolean executeForOutput(List<String> command, StringBuilder outputBuilder)
      throws IOException {
    LOGGER.debug("Executing command: " + StringUtils.join(command, " "));
    ProcessBuilder pb = new ProcessBuilder(command)
        .redirectError(ProcessBuilder.Redirect.INHERIT)
        .redirectOutput(ProcessBuilder.Redirect.PIPE);

    Process p = pb.start();
    try {
      BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        String lineWithSep = line + System.getProperty("line.separator");
        outputBuilder.append(lineWithSep);
      }
      int exitCode = p.waitFor();
      return exitCode == Shell.SUCCESS;
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      p.destroy();
      throw new IOException("Failed to read process output", e);
    } catch (IOException e) {
      p.destroy();
      throw e;
    }
  }
}

@@ -1798,39 +1798,36 @@
 uilder.Redirect.
-INHERIT
+PIPE
 )%0A        .redir
@@ -1916,90 +1916,307 @@
     
-BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
+int exitCode = p.waitFor();%0A      boolean success = exitCode == Shell.SUCCESS;%0A      BufferedReader in;%0A      if (success) %7B%0A        in = new BufferedReader(new InputStreamReader(p.getInputStream()));%0A      %7D else %7B%0A        in = new BufferedReader(new InputStreamReader(p.getErrorStream()));%0A      %7D
 %0A   
@@ -2413,74 +2413,22 @@
     
-int exitCode = p.waitFor();%0A      return exitCode == Shell.SUCCESS
+return success
 ;%0A  

Read the stderr for finding more granular error types