Groosh

Provides a shell-like capability for handling external processes.

Module Overview

Groosh is a Unix like shell written in Groovy. It has also been known as the Process Module but was renamed (back) when it was updated to work with Groovy 1.0.

The current version of Groosh is 0.3.0

Team Members

  • Yuri Schimke - Founder and original contributor
  • Alexander Egger [alexander.egger at campus02.at] - Current maintainer

Download

Distributions

Source and binary releases are available at http://svn.codehaus.org/groovy-contrib/groosh/releases.

A RPM package for openSUSE 10.2 can be installed via the openSUSE Build Service repository http://download.opensuse.org/repositories/home:eggeral/openSUSE_10.2.

A RPM package for openSUSE 10.3 can be installed via the openSUSE Build Service repository http://download.opensuse.org/repositories/home:eggeral/openSUSE_10.3.

Installing

If you use the openSUSE RPM, just install it and you are ready to go.

If you use the binary distribution e.g. (groosh-0.3.0-bin.tar.gz).
Untar it and copy groosh-0.3.0.jar to your $GROOVY_HOME/lib.

Try the following script to check if it worked:

def gsh = new groosh.Groosh;
gsh.ls().toStdOut();

Have a look at the examples directory for more examples how to use
groosh.

Pre-requisites

Groosh is based on Groovy 1.5.0 or higher.

Documentation

The following example shows Groosh in action:

def gsh = new groosh.Groosh();

gsh.cat('test_scripts/blah.txt').toStdOut();

Another example :

def gsh = new groosh.Groosh();

def f = gsh.find('.', '-name', '*.java', '-ls');
def total = 0;
def lines = gsh.grid { values,w |
  def x = values[2,4,6,10];
  def s = x.join('	');
  w.println(s);
  def total += Integer.parseInt(values[6]);
};

f.pipeTo(lines);
lines.toStdOut();

println "Total: " + tota);

Sometimes the name of a shell command conflicts with a Groovy method (for example ''grep''). This means that

gsh.grep(...)

does not execute the shell command, but the Groovy method grep(...).

As a workaround for that you may prefix any shell command with _ this means the example above becomes

gsh._grep(...)

The following example shows a more elaborate example. It uploads photos to a flickr account using the command line tool flickcurl. A photo set of this images is created and named after the current directory.

import static groosh.Groosh.groosh
import static org.codehaus.groovy.groosh.stream.DevNull.devnull

ids = [:]
shell = groosh()

//get all images in this folder and upload it to flickr
//remember the photo id we get from flickr
shell.ls().grep(~/.*jpg/).each {
  println "Uploading file $it to flickr"
  flickcurl = shell.flickcurl("upload",it,"friend","family").useError(true)
  id = flickcurl.grep(~/.*Photo ID.*/)[0].split(":")[1].trim()
  ids[it] = id
  println "Photo ID is: $id"
}
//we need to know the first photo id
firstKey = ids.keySet().toList()[0]

//create a set with the name of the directory we are in right now
//use the id of the first photo as set cover
setName = shell.pwd().text.split("/")[-1]
println "Creating set: $setName"
flickcurl = shell.flickcurl("photosets.create",setName,setName,ids[firstKey]).useError(true)
id = flickcurl.grep(~/.*Photoset.*/)[0].split(" ")[2].trim()
println "Photoset ID is: $id"

//make a backup of the ids in a file for later reference
println "Writing ids to a file"
file = new File(shell.pwd().text.trim() + "/.flickrset")
file << "Photoset:" << id << "\n"
ids.each {
  file << it.key << ":" << it.value << "\n"    
}

//the first photo is allready part of the photo set so lets remove it
ids.remove(firstKey)

//add the remaining photos to the photo set
ids.each {
  println "Adding photo to set at flickr: $it"
  shell.flickcurl("photosets.addPhoto",id,it.value) | devnull()
}

println "DONE"

Developers

Source Control

The Groosh source code is available from the Codehaus SVN at http://svn.codehaus.org/groovy-contrib/groosh/trunk.

Building

Building from the source distribution

Untar/unzip the source distribution file (e.g. groosh-0.3.0-src.tar.gz)

Groosh uses Maven 2 for building. You have to have Maven 2 installed.
Just execute mvn in the groosh directory and groosh-0.3.0.jar gets build
as well as the source and binary distribution.

Building the SVN version

Check out the source from
http://svn.codehaus.org/groovy-contrib/groosh/trunk/

Building works the same as for the source distribution.

Contributing

Please contact Alexander Egger [alexander.egger at campus02.at] by e-mail.

Community

Mailing List(s)

http://groovy.codehaus.org/Mailing+Lists

Issue tracker

http://jira.codehaus.org/secure/BrowseProject.jspa?id=10242

Labels

 
(None)