I heart groovy

Posted: January 19th, 2011 | Author: | Filed under: code | Tags: | Comments Off on I heart groovy

Reason number 2343242354 why i love groovy – I had a bunch of urls and I needed to run NSLOOKUP on them. I had a couple of options, write a shell script, do each by hand, write it in java (eeek) or write it in groovy. I chose groovy.

"https://sealserver.trustkeeper.net"].each{ url ->
println "nslookup ${url[8..-1]}".execute().text

Ok let’s look whats going on here. First thing you’ll notice if you’re coming from the java world is there is no class definition, there’s an implicit class created for you so for simple scripts especially run once scripts like this, it’s not necessary.

The first part is a list which is created with []


Next we are calling a method on the list called .each which accepts a closure which is in this case

{ url ->
println "nslookup ${url[8..-1]}".execute().text

Contained in this closure is what I really needed to do, which was do an NSLOOKUP on each url, but only the domain, I had to strip out the protocol (I know this could have easily been done with a search and replace in my editor but then you don’t have any example code)

We start by creating a groovy string – denoted by the"" which allows us to substitute the url in with the ${url} syntax. Another niceity is to omit the {}’s if you do not need to run any additional methods on the variable you are substituting – had the https:// not been in the url i could have written it like so:

println "nslookup $url".execute().text

What you’ll notice is by using the array index notation with a range I am able to get all chars starting at position 8 and go all the way to the end (in groovy -1 means the end of the string, -2 would be 2 from the right -3, 3 from the right etc. – no more subString(pos, string.len - pos)) This same syntax also works with lists

Now that I have the portion of the domain I need, we call .execute() on it. In groovy a .execute method is added to the String class and when doing so the entire string will be passed to the command line and executed. The following would print out the directory listing where the script was running.

"ls -la".execute().text

Finally we call .text (equivalent of .getText()) since .execute only returns a java.lang.UNIXProcess and the text property gives you back the output

Also note that println is the equivalent of System.out.println("text") – it’s just another groovy shortcut.

Pods Fail

Posted: November 30th, 2008 | Author: | Filed under: code | Tags: , , , | 5 Comments »

Always on the lookout for new tools I thought I’d try and install pods.  Pods claims to be a simple cms tool that allows you to create relatable objects. You can create master > detail pages, calendars, photo albums. Seems to be the simple “relational db” plugin I’ve been looking for.

The install instructions are:

  1. Copy the pods .htaccess file and overwrite yours
  2. Change the permalink to /%postname%/
  3. Activate plugin

I followed those instructions and got the following warnings

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /purpleasphalt/wp-content/plugins/pods/init.php on line 33

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /purpleasphalt/wp-content/plugins/pods/sql/update.php on line 4

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /purpleasphalt/wp-content/plugins/pods/sql/update.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /purpleasphalt/wp-content/plugins/pods/init.php:33) in /purpleasphalt/wp-includes/classes.php on line 1569

On I trudged ignoring the errors. I was able to create a new pod, or at least it showed up on the screen, but when I refreshed, it was gone.

I started poking around the comments section of the site and found that the warnings would be cleared up in the next release. I also noted that issues can be cleared up by removing the associated pods tables, and that they would be re-created. However it looks like from my warnings that pods could not determine if it had already installed itself. My suspicions were correct when I checked mysql and found that there were no pods tables found.

I’ll post more when 1.2.1 hits and the warnings go away.