/** * Creates an input stream of text as UTF-8 bytes. * @param text The text which will be treated as UTF-8 bytes * @return The new input */ static public StreamableInput input(String text) { return input(text, StandardCharsets.UTF_8); }
static public StreamableOutput output(OutputStream stream) { return output(stream, null); }
public SshExec(Context context, SshSession session) { super(context); this.pipeInput = Streamables.standardInput(); this.pipeOutput = Streamables.standardOutput(); this.pipeError = Streamables.standardError(); this.pipeErrorToOutput = false; this.session = session; this.arguments = new ArrayList<>(); this.pty = false; this.exitValues = new ArrayList<>(Arrays.asList(0)); }
static private Map<String,String> which(SshSession ssh, List<String> commands) { // doesn't matter if we find it or not String whichString = sshExec(ssh, "which", commands.toArray()) .pipeOutput(Streamables.captureOutput()) .pipeError(Streamables.nullOutput()) .exitValues(0, 1, 2) .runResult() .map(Actions::toCaptureOutput) .asString(); if (whichString == null) { return Collections.emptyMap(); } Map<String,String> result = new HashMap<>(); String[] lines = whichString.split("\\\n"); for (String line : lines) { Path cmd = Paths.get(line.trim()); result.put(cmd.getFileName().toString(), line.trim()); } return result; }
public Pipeline add(PipeMixin pipable) { if (!(pipable instanceof Action)) { throw new IllegalArgumentException("pipable must be an instance of " + Action.class.getCanonicalName()); } if (this.pipables.size() > 0) { PipeMixin lastPipable = this.pipables.get(this.pipables.size() - 1); // connect output to input log.debug("Connecting {} output -> {} input", lastPipable.getClass(), pipable.getClass()); BytePipe pipe = new BytePipe(); lastPipable.pipeOutput(Streamables.output(pipe.getOutputStream(), "<pipe>")); pipable.pipeInput(Streamables.input(pipe.getInputStream(), "<pipe>")); } this.pipables.add(pipable); return this; }
Streamables.copy(pipable.getPipeInput(), lineOutput); } catch (IOException e) { throw new WrappedBlazeException(e); Streamables.close(pipable.getPipeInput()); Streamables.close(lineOutput); throw new WrappedBlazeException(e); } finally { Streamables.closeQuietly(pipable.getPipeOutput());
/** * Helper method to make it easier to exec a program and capture its output. * @return The captured output * @throws BlazeException */ default public CaptureOutput runCaptureOutput() throws BlazeException { CaptureOutput captureOutput = null; StreamableOutput output = getPipeOutput(); // already set as capture output? if (output != null && output instanceof CaptureOutput) { captureOutput = (CaptureOutput)output; } else { captureOutput = Streamables.captureOutput(); this.pipeOutput(captureOutput); } this.run(); return captureOutput; }
static public void copy(StreamableInput input, StreamableOutput output) throws IOException { copy(input, output, 16384); }
static public void closeQuietly(Closeable stream) { try { close(stream); } catch (BlazeException e) { // do nothing } }
@Override public void stop() { // NOTE: travis ci deadlocks unless we add this -- never happens // on a real system so its pretty odd Thread.yield(); // make sure any input, output, and error streams are closed // before the superclass stop() is triggered Streamables.closeQuietly(is); //Streamables.closeQuietly(os); //Streamables.closeQuietly(es); super.stop(); } };
static public Stream<String> lines(StreamableInput input) { BufferedReader br = new BufferedReader(new InputStreamReader(input.stream)); try { return br.lines().onClose(asUncheckedRunnable(br)); } catch (Error|RuntimeException e) { try { br.close(); } catch (IOException ex) { try { e.addSuppressed(ex); } catch (Throwable ignore) {} } throw e; } }
public Integer getGroupId(String group) { try { String groupId = sshExec(false, false, "id", "-g", group) .pipeOutput(Streamables.captureOutput()) .pipeError(Streamables.nullOutput()) .runResult() .map(Actions::toCaptureOutput) .asString() .trim(); return Integer.valueOf(groupId); } catch (UnexpectedExitValueException e) { return null; } }
public Pipeline add(PipeMixin pipable) { if (!(pipable instanceof Action)) { throw new IllegalArgumentException("pipable must be an instance of " + Action.class.getCanonicalName()); } if (this.pipables.size() > 0) { PipeMixin lastPipable = this.pipables.get(this.pipables.size() - 1); // connect output to input log.debug("Connecting {} output -> {} input", lastPipable.getClass(), pipable.getClass()); BytePipe pipe = new BytePipe(); lastPipable.pipeOutput(Streamables.output(pipe.getOutputStream(), "<pipe>")); pipable.pipeInput(Streamables.input(pipe.getInputStream(), "<pipe>")); } this.pipables.add(pipable); return this; }
Streamables.copy(pipable.getPipeInput(), lineOutput); } catch (IOException e) { throw new WrappedBlazeException(e); Streamables.close(pipable.getPipeInput()); Streamables.close(lineOutput); throw new WrappedBlazeException(e); } finally { Streamables.closeQuietly(pipable.getPipeOutput());
/** * Helper method to make it easier to exec a program and capture its output. * @return The captured output * @throws BlazeException */ default public CaptureOutput runCaptureOutput() throws BlazeException { CaptureOutput captureOutput = null; StreamableOutput output = getPipeOutput(); // already set as capture output? if (output != null && output instanceof CaptureOutput) { captureOutput = (CaptureOutput)output; } else { captureOutput = Streamables.captureOutput(); this.pipeOutput(captureOutput); } this.run(); return captureOutput; }
static private void copy(InputStream input, OutputStream output) throws IOException { copy(input, output, 16384); }
static public void closeQuietly(Closeable stream) { try { close(stream); } catch (BlazeException e) { // do nothing } }
@Override public void stop() { // NOTE: travis ci deadlocks unless we add this -- never happens // on a real system so its pretty odd Thread.yield(); // make sure any input, output, and error streams are closed // before the superclass stop() is triggered Streamables.closeQuietly(is); //Streamables.closeQuietly(os); //Streamables.closeQuietly(es); super.stop(); } };
static public Stream<String> lines(StreamableInput input) { BufferedReader br = new BufferedReader(new InputStreamReader(input.stream)); try { return br.lines().onClose(asUncheckedRunnable(br)); } catch (Error|RuntimeException e) { try { br.close(); } catch (IOException ex) { try { e.addSuppressed(ex); } catch (Throwable ignore) {} } throw e; } }
public Exec(Context context) { super(context); // which will be used to locate the executable this.which = new Which(context); this.arguments = new ArrayList<>(); this.executor = new ProcessExecutor() .exitValueNormal(); this.pipeInput = Streamables.standardInput(); this.pipeOutput = Streamables.standardOutput(); this.pipeError = Streamables.standardError(); this.exitValues = new ArrayList<>(); this.exitValues.add(0); }