Exit Full View

Feather2 / feather2-runtime / src / main / java / uk / co / nickthecoder / feather / runtime / command / Commandable.java

package uk.co.nickthecoder.feather.runtime.command;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public interface Commandable {

    /**
     * Note, this returns the process of the `sh -c` process, NOT the process of the command itself.
     * Using `Process.destroy` will only destroy the `sh` process, not the command's process.
     */
    Process runAsync(Consumer outConsumer, Consumer errConsumer) throws IOException;

    /**
     * Note, this returns the process of the `sh -c` process, NOT the process of the command itself.
     * Using `Process.destroy` will only destroy the `sh` process, not the command's process.
     */
    Process runAsync() throws IOException;

    /**
     * Runs the command, and waits for it to end.
     * stdout and stderr are inherited from this (JVM) process.
     */
    CommandResult run();

    /**
     * Runs the command, and waits for it to end.
     * If either of stdout or stderr are null, then the streams are inherited from this (JVM) process.
     */
    CommandResult run(Consumer stdout, Consumer stderr);

    /**
     * Runs the command, and also collects the standard output and standard error of the process into String objects.
     * Note, if the command outputs lots of text, then you should NOT use this, as you
     * will have a huge String object.
     * Instead, use CommandRunner.run(Command, Consumer, Consumer).
     *
     * @return The CommandResult, from which you can get the exit status, stdout, stderr etc.
     */
    CommandResult collect();

    /**
     * A convenience method, which is the same as Command.collect().out
     * Note, if the command outputs lots of text, then you should NOT use this, as you
     * will have a huge String object.
     * Instead, use CommandRunner.run(Command, Consumer, Consumer).
     *
     * @return The standard output of the command as a String.
     */
    public String eval();

    /**
     * Similar to `eval()`, but this also checks the exit status.
     *
     * @return null if the exit status is non-zero, otherwise, the standard output of the command.
     */
    String evalOk();


    CommandRunner dir(File directory);

    CommandRunner dir(String directoryName);

    CommandRunner env(Map<String, String> env);

    CommandRunner shell(Shell shell);
}