Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A framework for building CLIs for REST APIs very easily.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 maven-plugin
Octocat-spinner-32 runtime
Octocat-spinner-32 .gitattributes
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.md
Octocat-spinner-32 pom.xml
README.md

Mule Jockey is a simple way to build CLIs using Groovy, with a focus on making it especially easy to interact with remote REST services.

Given a command like this:

import com.mulesoft.jockey.*;  
@Command("test")
@Options([
  @Option(name="n",longName="name",description="Your name",optional=false)
])
public class TestCommand extends GroovyCommand {
    void execute() {
        println "Hello " + name;
    }
}

Mule Jockey will create a test.sh and a test.bat file inside a distribution that will execute it. It will also take the command line parameter -n or --name and inject it into your command as a name field.

Quick Start

1) Create your project

$ mkdir testcli
$ cd testcli

2) Create a pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mulesoft.ion</groupId>
    <artifactId>testcli</artifactId>
    <name>Test CLI tools</name>
    <version>1.0</version>  
    <dependencies>
        <dependency>
            <groupId>com.mulesoft</groupId>
            <artifactId>mule-jockey</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>  
    <build>
        <plugins>
            <plugin>
                <groupId>com.mulesoft</groupId>
                <artifactId>mule-jockey-maven-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <configuration>
                    <distributionName>ion-cli-${version}</distributionName>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>  
    <repositories>
        <repository>
            <id>java.net</id>
            <url>http://download.java.net/maven/2/</url>
        </repository>
        <repository>
            <id>mule-releases</id>
            <url>http://repository.muleforge.org/release/</url>
        </repository>
        <repository>
            <id>mule-snapshots</id>
            <name>Mule Snapshot Repository</name>
            <url>http://repository.muleforge.org/snapshot/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

3) Create your source directory

$ mkdir src/main/groovy  

4) Edit your first command, testcli/src/main/groovy/TestCommand.groovy and enter the following:

import com.mulesoft.jockey.*;  
@Command("test")
@Options([
  @Option(name="n",longName="name",description="Your name",optional=false)
])
public class TestCommand extends GroovyCommand {
    void execute() {
        println "Hello " + name;
    }
}

5) Build your CLI

$ mvn install

6) Test it out:

$ unzip target/test-1.0.zip
$ cd test-1.0
$ ./bin/test.sh -h 
usage: test
 -h,--help         Print out help for this command.
 -n,--name <arg>   Your name
$ ./bin/test.sh -n Dan
Hello Dan

Writing HTTP Based Commands

Mule Jockey will inject a copy of a Jersey client into your commands so that you can easily talk to RESTful services. This makes it extremely easy to talk to JSON based services. For example:

import com.mulesoft.jockey.*
import com.sun.jersey.api.client.ClientResponse  

@Command("list-some-things")
@Options([
    @Option(name="x", longName="url", description="URL", defaultValue="http://localhost/api"),
    @Option(name="u", longName="username", description="Username", defaultValue="admin"),
    @Option(name="p", longName="password", description="Password", defaultValue="admin"),
])
public class ListSomeThings extends  {
    void execute() {
        client.addFilter(new HTTPBasicAuthFilter(username, password));

        def response = client.resource("${url}/somethings")
                             .get(ClientResponse.class);

        def values = response.getEntity(Map[].class);
        values.each { 
            println "${it}";
        }
    }
}

See here for more info about the Jersey Client API: http://jersey.java.net/nonav/apidocs/latest/jersey/index.html

Something went wrong with that request. Please try again.