Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / bin / rt-crontool.in
index 383014f..a8fc30d 100644 (file)
@@ -51,23 +51,15 @@ use warnings;
 use Carp;
 
 # fix lib paths, some may be relative
-BEGIN {
+BEGIN { # BEGIN RT CMD BOILERPLATE
     require File::Spec;
+    require Cwd;
     my @libs = ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@");
     my $bin_path;
 
     for my $lib (@libs) {
         unless ( File::Spec->file_name_is_absolute($lib) ) {
-            unless ($bin_path) {
-                if ( File::Spec->file_name_is_absolute(__FILE__) ) {
-                    $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
-                }
-                else {
-                    require FindBin;
-                    no warnings "once";
-                    $bin_path = $FindBin::Bin;
-                }
-            }
+            $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
             $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
         }
         unshift @INC, $lib;
@@ -79,10 +71,7 @@ use RT;
 
 use Getopt::Long;
 
-use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
-
-#Clean out all the nasties from the environment
-CleanEnv();
+use RT::Interface::CLI qw(GetCurrentUser loc);
 
 my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg,
      $template, $template_id, $transaction, $transaction_type, $help, $log, $verbose );
@@ -106,7 +95,7 @@ GetOptions(
 RT::LoadConfig();
 
 # adjust logging to the screen according to options
-RT->Config->Set( LogToScreen => $log ) if $log;
+RT->Config->Set( LogToSTDERR => $log ) if $log;
 
 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
 RT::Init();
@@ -121,7 +110,7 @@ my $CurrentUser = GetCurrentUser();
 help() if $help;
 
 unless ( $CurrentUser->Id ) {
-    print loc("No RT user found. Please consult your RT administrator.");
+    print loc("No RT user found. Please consult your RT administrator.") . "\n";
     exit(1);
 }
 
@@ -261,24 +250,20 @@ sub get_transactions {
 # 
 # =cut
 
-{ my $cache = undef;
 sub get_template {
     my $ticket = shift;
     return undef unless $template;
 
     unless ( $template =~ /\D/ ) {
         # by id
-        return $cache if $cache;
-
-        my $cache = RT::Template->new( RT->SystemUser );
-        $cache->Load( $template );
+        my $template_obj = RT::Template->new( RT->SystemUser );
+        $template_obj->Load( $template );
         die "Failed to load template '$template'"
-            unless $cache->id;
-        return $cache;
+            unless $template_obj->id;
+        return $template_obj;
     }
 
     my $queue = $ticket->Queue;
-    return $cache->{ $queue } if $cache->{ $queue };
 
     my $res = RT::Template->new( RT->SystemUser );
     $res->LoadQueueTemplate( Queue => $queue, Name => $template );
@@ -287,8 +272,8 @@ sub get_template {
         die "Failed to load template '$template', either for queue #$queue or global"
             unless $res->id;
     }
-    return $cache->{ $queue } = $res;
-} }
+    return $res;
+}
 
 
 # =head2 load_module
@@ -299,9 +284,9 @@ sub get_template {
 
 sub load_module {
     my $modname = shift;
-    eval "require $modname";
-    if ($@) {
-        die loc( "Failed to load module [_1]. ([_2])", $modname, $@ );
+    unless ($modname->require) {
+        my $error = $@;
+        die loc( "Failed to load module [_1]. ([_2])", $modname, $error );
     }
 
 }
@@ -313,37 +298,37 @@ sub help {
       . "\n";
     print loc("It takes several arguments:") . "\n\n";
 
-    print "    "
+    print "        "
       . loc( "[_1] - Specify the search module you want to use", "--search" )
       . "\n";
-    print "    "
+    print "        "
       . loc( "[_1] - An argument to pass to [_2]", "--search-arg", "--search" )
       . "\n";
 
-    print "    "
+    print "        "
       . loc( "[_1] - Specify the condition module you want to use", "--condition" )
       . "\n";
-    print "    "
+    print "        "
       . loc( "[_1] - An argument to pass to [_2]", "--condition-arg", "--condition" )
       . "\n";
-    print "    "
+    print "        "
       . loc( "[_1] - Specify the action module you want to use", "--action" )
       . "\n";
-    print "    "
+    print "        "
       . loc( "[_1] - An argument to pass to [_2]", "--action-arg", "--action" )
       . "\n";
-    print "    "
+    print "        "
       . loc( "[_1] - Specify name or id of template(s) you want to use", "--template" )
       . "\n";
-    print "    "
+    print "        "
       . loc( "[_1] - Specify if you want to use either 'first', 'last' or 'all' transactions", "--transaction" )
       . "\n";
-    print "    "
+    print "        "
       . loc( "[_1] - Specify the comma separated list of transactions' types you want to use", "--transaction-type" )
       . "\n";
-    print "    "
-      . loc( "[_1] - Adjust LogToScreen config option", "--log" ) . "\n";
-    print "    "
+    print "        "
+      . loc( "[_1] - Adjust LogToSTDERR config option", "--log" ) . "\n";
+    print "        "
       . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
     print "\n";
     print "\n";
@@ -458,7 +443,7 @@ Specify the comma separated list of transactions' types you want to use
 
 =item log
 
-Adjust LogToScreen config option
+Adjust LogToSTDERR config option
 
 =item verbose