package org.implab.gradle.common.exec;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

public interface Runnable {
    CompletableFuture<Integer> run() throws IOException;

    /** Starts process and will throw error if error code is non-zero */
    default CompletableFuture<Integer> run(boolean throwOnError) throws IOException {
        return run().thenApply(code -> {
            if (!throwOnError && code != 0)
            throw new CompletionException(new IOException(
                    String.format("The process is terminated with code %d", code)));
            return code;
        });
    }
}
