Take a ride in the CLI with Mule Jockey

motif

How many web sites/web services have you wished you could just interact with over the command line? Sometimes, you just want to type commands in your shell. I can name at least 3 of our products which I’ve wished I could do that with: iON, MMC, and Tcat.

There are some challenges though. Bash/BAT file scripts don’t provide facilities to interact with web services. Then, if you go with a cross platform language, you have to write cross platform scripts and provide a way to distribute it.

We thought we’d make this easier, so we wrote Mule Jockey, a tool to turn annotated groovy scripts into a cross platform distribution of shell scripts.

It’s really easy. First, write your command. For example:

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;
    }
}

You specify the name of your command via the @Command annotation and options via the @Option annotation. All your options get injected into your application using the longName attribute.

Then you need to create a POM file and run the Maven plugin. See an example here. Run “mvn package” and presto, you have a CLI distribution with scripts which will run on both Windows and *nix.

Now, let’s run your command:

Ok, but what about interacting with web services? Well, that’s pretty easy because we inject the Jersey client into your command for you to use. Check out this command:

Here we’re combining the power of a dynamic language and Jersey client to easily print out the statuses from the Twitter public timeline. You simply read in the JSON response as a list of Maps and refer to all the properties, such as the tweet text, as fields on that object.

You can also take advantage of and Jersey to do easy posting of JSON objects. Here’s an example creates a twitter update object and does an HTTP POST.

Could it get any easier?

We hope that you’ll find Mule Jockey helpful and send us along your feedback . If you’d like to get started using Mule Jersey, you can read the documentation or download the source at the github site.


We'd love to hear your opinion on this post


One Response to “Take a ride in the CLI with Mule Jockey”

  1. “If you’d like to get started using Mule Jockey” 🙂

    Good article.