[Bps-public-commit] r14441 - in Prophet/trunk: t

simonw at bestpractical.com simonw at bestpractical.com
Wed Jul 23 17:24:59 EDT 2008


Author: simonw
Date: Wed Jul 23 17:24:58 2008
New Revision: 14441

Modified:
   Prophet/trunk/lib/Prophet/CLI.pm
   Prophet/trunk/lib/Prophet/Config.pm
   Prophet/trunk/t/config.t
   Prophet/trunk/t/test_app.conf

Log:
Add 'alias' decorator to config files that allows you to alias command lines 


Modified: Prophet/trunk/lib/Prophet/CLI.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/CLI.pm	(original)
+++ Prophet/trunk/lib/Prophet/CLI.pm	Wed Jul 23 17:24:58 2008
@@ -97,8 +97,13 @@
 sub _get_cmd_obj {
     my $self = shift;
 
-    my @commands = map { exists $CMD_MAP{$_} ? $CMD_MAP{$_} : $_ }
-                   @{ $self->primary_commands };
+    my $aliases  = $self->app_handle->config->aliases;
+    my $tmp      = $self->primary_commands;
+    if ($aliases->{$tmp->[0]}) {
+        @ARGV = split ' ', $aliases->{$tmp->[0]};
+        return $self->run_one_command;
+    }
+    my @commands = map { exists $CMD_MAP{$_} ? $CMD_MAP{$_} : $_ } @{ $tmp };
 
     my @possible_classes;
 

Modified: Prophet/trunk/lib/Prophet/Config.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Config.pm	(original)
+++ Prophet/trunk/lib/Prophet/Config.pm	Wed Jul 23 17:24:58 2008
@@ -17,18 +17,22 @@
 );
 
 has config => (
-    metaclass => 'Collection::Hash',
-    is        => 'rw',
-    isa       => 'HashRef',
-    lazy      => 0,
-    default   => sub { shift->load_from_files },
-    provides  => {
-        get   => 'get',
-        set   => 'set',
-        keys  => 'list'
+    metaclass   => 'Collection::Hash',
+    is          => 'rw',
+    isa         => 'HashRef',
+    lazy        => 0,
+    default     => sub { shift->load_from_files },
+    provides    => {
+        get     => 'get',
+        set     => 'set',
+        keys    => 'list',
     },
 );
 
+sub aliases {
+    return $_[0]->config->{_aliases};
+}
+
 #sub prophet_config_file { dir($ENV{HOME}, ".prophetrc") }
 sub app_config_file { 
     my $self = shift;
@@ -63,7 +67,13 @@
     for my $line ($file->slurp) {
         $line =~ s/\#.*$//; # strip comments
         next unless ($line =~ /^([^:]+?)\s*=\s*(.*)$/);
-            $config->{$1} = $2;
+        my $key = $1;
+        my $val = $2;
+        if ($key =~ m!alias\s+(.+)!) {
+            $config->{_aliases}->{$1} = $val;
+        } else { 
+            $config->{$key} = $val;
+        }
     }
 }
 

Modified: Prophet/trunk/t/config.t
==============================================================================
--- Prophet/trunk/t/config.t	(original)
+++ Prophet/trunk/t/config.t	Wed Jul 23 17:24:58 2008
@@ -17,6 +17,7 @@
 can_ok($config, 'get');
 can_ok($config, 'set');
 can_ok($config, 'list');
+can_ok($config, 'aliases');
 
 is($config->get('_does_not_exist'), undef);
 is($config->set('_does_not_exist' => 'hey you!'), 'hey you!');
@@ -40,8 +41,11 @@
 my $conf = Prophet::Config->new(app_handle => Prophet::CLI->new->app_handle);
 # interrogate its config to see if we have any config options set
 my @keys = $conf->list;
-is (scalar @keys,2);
+is (scalar @keys,3);
+# test the alias
+is($conf->aliases->{tlist}, "ticket list", "Got correct alias");
 }
 
+
 # run the cli "show config" command 
 # make sure it matches with our file

Modified: Prophet/trunk/t/test_app.conf
==============================================================================
--- Prophet/trunk/t/test_app.conf	(original)
+++ Prophet/trunk/t/test_app.conf	Wed Jul 23 17:24:58 2008
@@ -2,3 +2,5 @@
 foo=bar
 # nor is this
 re = rawr
+# This is an alias
+alias tlist = ticket list



More information about the Bps-public-commit mailing list