Skip to content

Instantly share code, notes, and snippets.

@dustymabe
Created July 29, 2016 19:50
Show Gist options
  • Save dustymabe/e4746598687fa5aee8f2e8ff6adbf7d2 to your computer and use it in GitHub Desktop.
Save dustymabe/e4746598687fa5aee8f2e8ff6adbf7d2 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class VagrantLauncher
{
public static void main(String args[])
{
try
{
String[] cmd = new String[3];
cmd[0] = args[0];
cmd[1] = "up";
cmd[2] = "--no-color";
// cmd[3] = "--debug";
Runtime rt = Runtime.getRuntime();
List<String> envp = new ArrayList<>();
for(Map.Entry<String, String> entry : System.getenv().entrySet()) {
envp.add(entry.getKey() + "=" + entry.getValue());
}
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
envp.add("SUB_USERNAME=" + args[2]);
envp.add("SUB_PASSWORD=" + args[3]);
Process proc = rt.exec(cmd, envp.toArray(new String[0]), new File(args[1]));
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("waitFor returned:" + exitVal);
System.exit(exitVal);
// outputGobbler.join();
// System.out.println("output thread died");
// errorGobbler.join();
// System.out.println("error thread died");
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment