com.g2forge.alexandria.command.IStandardCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ax-command Show documentation
Show all versions of ax-command Show documentation
Library for command line programming.
package com.g2forge.alexandria.command;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.file.Path;
import com.g2forge.alexandria.command.exit.IExit;
import com.g2forge.alexandria.command.stdio.StandardIO;
import com.g2forge.alexandria.java.core.helpers.HCollection;
import com.g2forge.alexandria.java.function.IFunction1;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@FunctionalInterface
public interface IStandardCommand extends IStructuredCommand {
@Data
@Builder
@AllArgsConstructor
public static class TestResult {
protected final IExit exit;
protected final InputStream standardOutput;
protected final InputStream standardError;
}
static void main(String[] args, IStandardCommand command) throws Throwable {
final CommandInvocation invocation = CommandInvocation.of(args);
final IExit exit = command.invoke(invocation);
System.exit(exit.getCode());
}
public static IStandardCommand of(IFunction1 super CommandInvocation, ? extends IConstructorCommand> factory) {
return invocation -> factory.apply(invocation).invoke();
}
public IExit invoke(CommandInvocation invocation) throws Throwable;
public default TestResult test(InputStream standardInput, Path working, String... arguments) throws Throwable {
final ByteArrayOutputStream standardOutput = new ByteArrayOutputStream();
final ByteArrayOutputStream standardError = new ByteArrayOutputStream();
final StandardIO io = new StandardIO<>(standardInput, new PrintStream(standardOutput), new PrintStream(standardError));
final CommandInvocation invocation = new CommandInvocation<>(HCollection.asList(arguments), io, working);
final IExit exit = invoke(invocation);
return new TestResult(exit, new ByteArrayInputStream(standardOutput.toByteArray()), new ByteArrayInputStream(standardError.toByteArray()));
}
}