Gearbox Command


Gearbox Command

Introducing Gearbox Command: a new API for running command line programs from Java. By making use of Java dynamic proxies we generate classes which perform command line operations from interfaces with annotations. Take a look at HelloWorld which runs echo "Hello, World!".

With the release of Alexandria 0.0.8 and Gearbox 0.0.3 we’ve introduced both Gearbox Command and a companion test library. This allows us to interact with, for example Maven in a type-safe and Java-friendly way in bulldozer release. This prevents us from having to mess with ProcessBuilder though we do use it under the covers. It also allows us to run commands transparently over SSH.

Hello World

Take a look at HelloWorld which runs echo "Hello, World!":

/**
 * A command interface for "echo". Note that the {@link ICommandInterface} parent is empty, and totally optional.
 */
public interface IEcho extends ICommandInterface {
	/**
	 * Run the "echo" command over the specified string arguments. Note that no annotations are necessary to
	 * control command line generation, because "echo" is such a simple command. For more complex commands
	 * you may need the annotations that go with {@link DumbCommandConverter}.
	 * 
	 * @param args The arguments to echo
	 * @return The output of echo from standard out.
	 */
	public String echo(String... args);
}

Testing

If you find yourself writing your own command interfaces, you should make use of ATestCommand for unit testing. It will help make sure your test cases are nice and minimal compared the below example, which is spelled out in detail so you can copy it directly into your main programs.

@Test
public void helloWorld() {
	// Create a new command proxy factory
	// Use the "dumb" converter to render command line arguments from method arguments
	// Use process builder to run the resulting commands
	final ICommandProxyFactory factory = new CommandProxyFactory(DumbCommandConverter.create(), new ProcessBuilderRunner(null));

	// Get a magical implementation of the interface
	final IEcho echo = factory.apply(IEcho.class);

	// Make sure it works correctly
	final String string = "Hello, World!";
	Assert.assertEquals(string, echo.echo(string).trim(/* We want to trim the new line that echo adds */));
}

Feedback

Try taking it for a spin, and don’t be shy about leaving us an issue for any help you might want.