Sunday 29 November 2009

Git - inhibit commit if modules don't compile

Sometimes I forget to run Perl base tests for my modules, before committing changes. As to what happens, I end up committing something that doesn't quite compile ;)

Luckily Git has a pre-commit hook one can use to run at least the "compile" tests.

The following aborts the commit if the t/00*.t tests (usually the "do all modules compile?" tests) in the repository don't run correctly:

~$ cat .git/hooks/pre-commit
#!/usr/bin/perl
# Runs modules' "compiles" tests before committing
# Dies (halting commit) if they don't compile
print "pre-commit => testing..\n";
do {qx{
prove -Ilib t/00*.t
}} or die <<'DIEMSG';
pre-commit => ERRORS:
$@
DIEMSG
print "pre-commit => test OK\n";


This is an example of a 00-load.t file, that can literally be dropped-in the t/ directory:

use strict;
use warnings;
use Test::More;
use File::Find::Rule;

my @files = File::Find::Rule->name('*.pm')->in('lib');
plan tests => ~ ~ @files;

for (@files) {
s/^lib.//;
s/.pm$//;
s{[\\/]}{::}g;

ok(
eval "require $_; 1",
"loaded $_ with no problems",
);
}


File::Find::Rule is one of the golden gems found on CPAN, rclamp++!

Till soon,
-marco-

Sunday 22 November 2009

CloudServers Perl module - 0.09_10

perl -E'say join " ", reverse world, hello' # :)


I've finally found some time to play again with the Rackspace API manual, and added a couple features to the Net-RackSpace-CloudServers module I hadn't touched since moving home some months ago.


The project is semi-alive on http://github.com/mfontani/Net-RackSpace-CloudServers/, and the latest 0.09_10 development version should be on CPAN soon.


It's basically a one-to-one adaptation of the Rackspace API document available at http://www.rackspacecloud.com/cloud_hosting_products/servers/api.


On the scripts/ directory there are some examples on how to use the module: it's possible to list all images, flavors, and servers you own, as well as delete servers by ID or create new servers in what I think is quite a simple syntax.


As an example, let's delete that pesky server whose ID's 666:

 
use strict; use warnings; use Net::RackSpace::CloudServers;
my $cs = Net::RackSpace::CloudServers->new(
user => $ENV{'CLOUDSERVERS_USER'},
key => $ENV{'CLOUDSERVERS_KEY'},
);
my @servers = $CS->get_server_detail;
my $srv_666 = ( grep { $_->id == 666 } @servers )[0];
die "No such server id 666\n" if ( !defined $srv_666 );
$srv_666->delete_server(); # dies in case of error
print "Server #666 deleted\n";


On a branch not on Github yet, I'm working on an App::Cmd interface to it, as well.


Any comments at all would be truly appreciated, as this is my first module I'm trying to get on CPAN ;)


Till soon,

-marco-