[Rt-commit] rt branch, 4.2/command-line-parsing, created. rt-4.2.9-57-g582d022

Alex Vandiver alexmv at bestpractical.com
Wed Dec 3 23:17:48 EST 2014


The branch, 4.2/command-line-parsing has been created
        at  582d022c17b8ea16391547896995da4e658cf2c5 (commit)

- Log -----------------------------------------------------------------
commit 1b35faa694211652482dec1c653077a8c0438e5e
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 16:53:28 2014 -0500

    Drop GetMessageContent; it is not currently called, and last was in RT2

diff --git a/bin/rt-crontool.in b/bin/rt-crontool.in
index dd57c23..079d934 100644
--- a/bin/rt-crontool.in
+++ b/bin/rt-crontool.in
@@ -71,7 +71,7 @@ use RT;
 
 use Getopt::Long;
 
-use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
+use RT::Interface::CLI qw(CleanEnv GetCurrentUser loc);
 
 my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg,
      $template, $template_id, $transaction, $transaction_type, $help, $log, $verbose );
diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 251b11c..05d2285 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -52,7 +52,7 @@ use warnings;
 use RT;
 
 use base 'Exporter';
-our @EXPORT_OK = qw(CleanEnv GetCurrentUser GetMessageContent debug loc);
+our @EXPORT_OK = qw(CleanEnv GetCurrentUser debug loc);
 
 =head1 NAME
 
@@ -63,7 +63,7 @@ our @EXPORT_OK = qw(CleanEnv GetCurrentUser GetMessageContent debug loc);
   use lib "/path/to/rt/libraries/";
 
   use RT::Interface::CLI  qw(CleanEnv
-                             GetCurrentUser GetMessageContent loc);
+                             GetCurrentUser loc);
 
   #let's talk to RT'
   use RT;
@@ -141,8 +141,6 @@ sub GetCurrentUser  {
     return($CurrentUser);
 }
 
-
-
 =head2 loc
 
   Synonym of $CurrentUser->loc().
@@ -156,70 +154,6 @@ sub loc {
 
 }
 
-
-
-=head2 GetMessageContent
-
-Takes two arguments a source file and a boolean "edit".  If the source file
-is undef or "", assumes an empty file.  Returns an edited file as an 
-array of lines.
-
-=cut
-
-sub GetMessageContent {
-    my %args = (  Source => undef,
-                  Content => undef,
-                  Edit => undef,
-                  CurrentUser => undef,
-                 @_);
-    my $source = $args{'Source'};
-
-    my $edit = $args{'Edit'};
-
-    my $currentuser = $args{'CurrentUser'};
-    my @lines;
-
-    use File::Temp qw/ tempfile/;
-
-    #Load the sourcefile, if it's been handed to us
-    if ($source) {
-        open( SOURCE, '<', $source ) or die $!;
-        @lines = (<SOURCE>) or die $!;
-        close (SOURCE) or die $!;
-    }
-    elsif ($args{'Content'}) {
-        @lines = split('\n',$args{'Content'});
-    }
-    #get us a tempfile.
-    my ($fh, $filename) = tempfile();
-
-    #write to a tmpfile
-    for (@lines) {
-        print $fh $_;
-    }
-    close ($fh) or die $!;
-
-    #Edit the file if we need to
-    if ($edit) {
-
-        unless ($ENV{'EDITOR'}) {
-            $RT::Logger->crit('No $EDITOR variable defined');
-            return undef;
-        }
-        system ($ENV{'EDITOR'}, $filename);
-    }
-
-    open( READ, '<', $filename ) or die $!;
-    my @newlines = (<READ>);
-    close (READ) or die $!;
-
-    unlink ($filename) unless (debug());
-    return(\@newlines);
-
-}
-
-
-
 sub debug {
     my $val = shift;
     my ($debug);
diff --git a/lib/RT/Interface/Email.pm b/lib/RT/Interface/Email.pm
index d2e9bb5..c3da469 100644
--- a/lib/RT/Interface/Email.pm
+++ b/lib/RT/Interface/Email.pm
@@ -66,7 +66,6 @@ BEGIN {
     # as well as any optionally exported functions
     @EXPORT_OK = qw(
         &CreateUser
-        &GetMessageContent
         &CheckForLoops
         &CheckForSuspiciousSender
         &CheckForAutoGenerated

commit 831674b0bbaaa5f08e496c2e2c21c3c038ae000e
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 17:03:11 2014 -0500

    $debug is never set; remove unreachable code

diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 05d2285..4d60987 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -156,15 +156,8 @@ sub loc {
 
 sub debug {
     my $val = shift;
-    my ($debug);
     if ($val) {
         $RT::Logger->debug($val);
-        if ($debug) {
-            print STDERR "$val\n";
-        }
-    }
-    if ($debug) {
-        return(1);
     }
 }
 

commit fa3c25daa8f58c47db5bb2594c1caff77da3b4f5
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 17:04:53 2014 -0500

    Deprecate unused and debug() sub

diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 4d60987..7ca890c 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -155,10 +155,8 @@ sub loc {
 }
 
 sub debug {
-    my $val = shift;
-    if ($val) {
-        $RT::Logger->debug($val);
-    }
+    RT->Deprecated( Remove => "4.4", Instead => '$RT::Logger->debug' );
+    $RT::Logger->debug(@_);
 }
 
 sub ShowHelp {

commit 8e36160f36317aba6c7f139467cdaa3b15dc2116
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 17:19:46 2014 -0500

    Modernize SYNOPSYS

diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 7ca890c..158a3ff 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -60,27 +60,20 @@ our @EXPORT_OK = qw(CleanEnv GetCurrentUser debug loc);
 
 =head1 SYNOPSIS
 
-  use lib "/path/to/rt/libraries/";
+  use lib "/opt/rt4/local/lib", "/opt/rt4/lib";
 
-  use RT::Interface::CLI  qw(CleanEnv
-                             GetCurrentUser loc);
+  use RT::Interface::CLI  qw(CleanEnv GetCurrentUser loc);
 
-  #let's talk to RT'
-  use RT;
-
-  #Load RT's config file
-  RT::LoadConfig();
-
-  # Connect to the database. set up loggign
-  RT::Init();
+  # Connect to the database, etc
+  use RT -init;
 
   # Clean out all the nasties from the environment
   CleanEnv();
 
-  #Get the current user all loaded
+  # Get the current user all loaded
   my $CurrentUser = GetCurrentUser();
 
-  print loc('Hello!'); # Synonym of $CuurentUser->loc('Hello!');
+  print loc('Hello!'); # Synonym of $CurrentUser->loc('Hello!');
 
 =head1 DESCRIPTION
 

commit a315e34f723895780e5e9842754e4738e6ebf1c0
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 17:20:56 2014 -0500

    Deprecate CleanEnv
    
    Removing these variables is only necessary if RT runs the shell; they
    were originally set in RT 1.0 when RT made common use of system() to
    pipe data to other programs.  RT now uses three-argument forms of
    open(), and thus bypasses the shell entirely.
    
    The function is left for backwards compatibility.

diff --git a/bin/rt-crontool.in b/bin/rt-crontool.in
index 079d934..40de4b9 100644
--- a/bin/rt-crontool.in
+++ b/bin/rt-crontool.in
@@ -71,7 +71,7 @@ use RT;
 
 use Getopt::Long;
 
-use RT::Interface::CLI qw(CleanEnv GetCurrentUser loc);
+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 );
@@ -100,9 +100,6 @@ RT->Config->Set( LogToSTDERR => $log ) if $log;
 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
 RT::Init();
 
-# Clean out all the nasties from the environment
-CleanEnv();
-
 require RT::Tickets;
 require RT::Template;
 
diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 158a3ff..18e0277 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -62,14 +62,11 @@ our @EXPORT_OK = qw(CleanEnv GetCurrentUser debug loc);
 
   use lib "/opt/rt4/local/lib", "/opt/rt4/lib";
 
-  use RT::Interface::CLI  qw(CleanEnv GetCurrentUser loc);
+  use RT::Interface::CLI  qw(GetCurrentUser loc);
 
   # Connect to the database, etc
   use RT -init;
 
-  # Clean out all the nasties from the environment
-  CleanEnv();
-
   # Get the current user all loaded
   my $CurrentUser = GetCurrentUser();
 
@@ -91,6 +88,8 @@ Removes some of the nastiest nasties from the user's environment.
 =cut
 
 sub CleanEnv {
+    RT->Deprecated( Remove => "4.4" );
+
     $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
     $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
     $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
diff --git a/sbin/rt-email-dashboards.in b/sbin/rt-email-dashboards.in
index 7c6cbbe..d6554a3 100644
--- a/sbin/rt-email-dashboards.in
+++ b/sbin/rt-email-dashboards.in
@@ -81,7 +81,7 @@ if ($opts{'help'}) {
 
 require RT;
 require RT::Interface::CLI;
-RT::Interface::CLI->import(qw{ CleanEnv loc });
+RT::Interface::CLI->import(qw{ loc });
 
 # Load the config file
 RT::LoadConfig();
@@ -89,9 +89,6 @@ RT::LoadConfig();
 # Connect to the database and get RT::SystemUser and RT::Nobody loaded
 RT::Init();
 
-# Clean out all the nasties from the environment
-CleanEnv();
-
 require RT::Dashboard::Mailer;
 RT::Dashboard::Mailer->MailDashboards(
     All    => $opts{all},
diff --git a/sbin/rt-email-digest.in b/sbin/rt-email-digest.in
index 40eac7a..11da949 100644
--- a/sbin/rt-email-digest.in
+++ b/sbin/rt-email-digest.in
@@ -68,12 +68,11 @@ BEGIN { # BEGIN RT CMD BOILERPLATE
 use Date::Format qw( strftime );
 use Getopt::Long;
 use RT;
-use RT::Interface::CLI qw( CleanEnv loc );
+use RT::Interface::CLI qw( loc );
 use RT::Interface::Email;
 
 RT::LoadConfig();
 RT::Init();
-CleanEnv();
 
 sub usage {
     my ($error) = @_;

commit b5e9dfc7f9faa49f1422638de94ba6a35316bd6e
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 19:27:50 2014 -0500

    Add a wrapper around Getopt::Long to add default --help option

diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 18e0277..70ba919 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -52,7 +52,7 @@ use warnings;
 use RT;
 
 use base 'Exporter';
-our @EXPORT_OK = qw(CleanEnv GetCurrentUser debug loc);
+our @EXPORT_OK = qw(CleanEnv GetCurrentUser debug loc Init);
 
 =head1 NAME
 
@@ -166,6 +166,51 @@ sub ShowHelp {
     );
 }
 
+=head2 Init
+
+A shim for L<Getopt::Long/GetOptions> which automatically adds a
+C<--help> option if it is not supplied.
+
+=cut
+
+sub Init {
+    require Getopt::Long;
+    require Pod::Usage;
+
+    my %exists;
+    my @args;
+    my $hash;
+    if (ref $_[0]) {
+        $hash = shift(@_);
+        for (@_) {
+            m/^([a-zA-Z0-9-]+)/;
+            $exists{$1}++;
+            push @args, $_ => \($hash->{$1});
+        }
+    } else {
+        $hash = {};
+        @args = @_;
+        while (@_) {
+            my $key = shift(@_);
+            $exists{$key}++;
+            shift(@_);
+        }
+    }
+
+    push @args, "help|h!" => \($hash->{help})
+        unless $exists{help};
+
+    my $ok = Getopt::Long::GetOptions( @args );
+    Pod::Usage::pod2usage(1) if not $ok and not defined wantarray;
+
+    return unless $ok;
+
+    Pod::Usage::pod2usage({ verbose => 2})
+          if not $exists{help} and $hash->{help};
+
+    return $ok;
+}
+
 RT::Base->_ImportOverlays();
 
 1;

commit c07f7a8f4f7fa823da573707e049be52e7507390
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 19:29:39 2014 -0500

    Init() also loads RT, removing more boilerplate for short scripts

diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 70ba919..410325e 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -49,7 +49,8 @@
 package RT::Interface::CLI;
 use strict;
 use warnings;
-use RT;
+
+use RT::Base;
 
 use base 'Exporter';
 our @EXPORT_OK = qw(CleanEnv GetCurrentUser debug loc Init);
@@ -62,10 +63,11 @@ our @EXPORT_OK = qw(CleanEnv GetCurrentUser debug loc Init);
 
   use lib "/opt/rt4/local/lib", "/opt/rt4/lib";
 
-  use RT::Interface::CLI  qw(GetCurrentUser loc);
+  use RT::Interface::CLI  qw(GetCurrentUser Init loc);
 
-  # Connect to the database, etc
-  use RT -init;
+  # Process command-line arguments, load the configuration, and connect
+  # to the database
+  Init();
 
   # Get the current user all loaded
   my $CurrentUser = GetCurrentUser();
@@ -169,7 +171,8 @@ sub ShowHelp {
 =head2 Init
 
 A shim for L<Getopt::Long/GetOptions> which automatically adds a
-C<--help> option if it is not supplied.
+C<--help> option if it is not supplied.  It then calls L<RT/LoadConfig>
+and L<RT/Init>.
 
 =cut
 
@@ -208,6 +211,12 @@ sub Init {
     Pod::Usage::pod2usage({ verbose => 2})
           if not $exists{help} and $hash->{help};
 
+    require RT;
+    RT::LoadConfig();
+    RT::Init();
+
+    $| = 1;
+
     return $ok;
 }
 

commit 3ce1ac4ee8fbc86ec6d68712835356cd7a305a32
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 19:30:04 2014 -0500

    Default to enabling error warnings to the screen for CLI tools
    
    Otherwise, serious errors may slip by, sent to the syslog where they go
    unnoticed.  For scripts which use Init(), this may now cause
    configuration errors (detected by PostLoadChecks) to be sent to STDERR.

diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 410325e..723bdb6 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -174,6 +174,9 @@ A shim for L<Getopt::Long/GetOptions> which automatically adds a
 C<--help> option if it is not supplied.  It then calls L<RT/LoadConfig>
 and L<RT/Init>.
 
+It sets the C<LogToSTDERR> setting to C<warning>, to ensure that the
+user sees all relevant warnings.
+
 =cut
 
 sub Init {
@@ -213,6 +216,9 @@ sub Init {
 
     require RT;
     RT::LoadConfig();
+
+    RT->Config->Set(LogToSTDERR => "warning");
+
     RT::Init();
 
     $| = 1;

commit a2afc4e49a0a67e15a3d950a0650995b3838c285
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 19:31:41 2014 -0500

    Allow standard --quiet and --verbose options via Init()
    
    These allow the adjustment of the STDERR logging level of CLI
    applications, to "error" and "debug", respectively.

diff --git a/lib/RT/Interface/CLI.pm b/lib/RT/Interface/CLI.pm
index 723bdb6..1214511 100644
--- a/lib/RT/Interface/CLI.pm
+++ b/lib/RT/Interface/CLI.pm
@@ -175,7 +175,9 @@ C<--help> option if it is not supplied.  It then calls L<RT/LoadConfig>
 and L<RT/Init>.
 
 It sets the C<LogToSTDERR> setting to C<warning>, to ensure that the
-user sees all relevant warnings.
+user sees all relevant warnings.  It also adds C<--quiet> and
+C<--verbose> options, which adjust the C<LogToSTDERR> value to C<error>
+or C<debug>, respectively.
 
 =cut
 
@@ -206,6 +208,12 @@ sub Init {
     push @args, "help|h!" => \($hash->{help})
         unless $exists{help};
 
+    push @args, "verbose|v!" => \($hash->{verbose})
+        unless $exists{verbose};
+
+    push @args, "quiet|q!" => \($hash->{quiet})
+        unless $exists{quiet};
+
     my $ok = Getopt::Long::GetOptions( @args );
     Pod::Usage::pod2usage(1) if not $ok and not defined wantarray;
 
@@ -217,7 +225,13 @@ sub Init {
     require RT;
     RT::LoadConfig();
 
-    RT->Config->Set(LogToSTDERR => "warning");
+    if (not $exists{quiet} and $hash->{quiet}) {
+        RT->Config->Set(LogToSTDERR => "error");
+    } elsif (not $exists{verbose} and $hash->{verbose}) {
+        RT->Config->Set(LogToSTDERR => "debug");
+    } else {
+        RT->Config->Set(LogToSTDERR => "warning");
+    }
 
     RT::Init();
 

commit 9d354baa1a434c619248605d860676e48b17737d
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 19:39:53 2014 -0500

    Switch interactive tools to use Init()
    
    Scripts which may be run from cron jobs and the like are not altered; as
    3ce1ac4e notes, the use of Init() may cause additional warnings to be
    logged.  While more correct, such a change is incorrect for a stable
    series; thus, only manually-run upgrade steps and command-line tools are
    altered.
    
    This also fixes a bug, introduced in 0d1cd53e, which caused the upgrade
    steps to no longer always log to STDERR.  In switching to:
    
        use RT -init;
        RT->Config->Set(LogToSTDERR => '...');
    
    The latter command did not alter the effective logging settings, as
    RT::Init had already been called.
    
    This commit changes the upgrade steps use Init(), and thus to default to
    displaying only "warning" levels to the user; the --verbose option is
    now available if debug output is still desired.

diff --git a/devel/tools/rt-attributes-editor b/devel/tools/rt-attributes-editor
index 92998a4..8a5cc7f 100755
--- a/devel/tools/rt-attributes-editor
+++ b/devel/tools/rt-attributes-editor
@@ -49,19 +49,12 @@
 use strict;
 use warnings;
 use Term::EditorEdit;
-use Getopt::Long;
-my ($help, $key, $id);
-GetOptions('help|h' => \$help, 'key|k=s' => \$key, 'id=i' => \$id);
 
-if ( $help || !$id ) {
-    require Pod::Usage;
-    Pod::Usage::pod2usage({ verbose => 2 });
-    exit;
-}
+use RT::Interface::CLI qw(Init);
+my ($key, $id);
+Init('key|k=s' => \$key, 'id=i' => \$id);
 
-require RT;
-RT::LoadConfig();
-RT::Init();
+Pod::Usage::pod2usage({ verbose => 2 }) unless $id;
 
 require RT::Attribute;
 my $attr = RT::Attribute->new( RT->SystemUser );
diff --git a/etc/upgrade/generate-rtaddressregexp.in b/etc/upgrade/generate-rtaddressregexp.in
index 84d8ad1..35369e4 100644
--- a/etc/upgrade/generate-rtaddressregexp.in
+++ b/etc/upgrade/generate-rtaddressregexp.in
@@ -53,10 +53,8 @@ use warnings;
 use lib "@LOCAL_LIB_PATH@";
 use lib "@RT_LIB_PATH@";
 
-use RT -init;
-RT->Config->Set('LogToSTDERR' => 'debug');
-
-$| = 1;
+use RT::Interface::CLI qw(Init);
+Init();
 
 if (my $re = RT->Config->Get('RTAddressRegexp')) {
     print "No need to use this script, you already have RTAddressRegexp set to $re\n";
diff --git a/etc/upgrade/sanity-check-stylesheets.in b/etc/upgrade/sanity-check-stylesheets.in
index ded82eb..0fe9300 100644
--- a/etc/upgrade/sanity-check-stylesheets.in
+++ b/etc/upgrade/sanity-check-stylesheets.in
@@ -53,10 +53,8 @@ use warnings;
 use lib "@LOCAL_LIB_PATH@";
 use lib "@RT_LIB_PATH@";
 
-use RT -init;
-RT->Config->Set('LogToSTDERR' => 'debug');
-
-$| = 1;
+use RT::Interface::CLI qw(Init);
+Init();
 
 use RT::Users;
 my $users = RT::Users->new( $RT::SystemUser );
diff --git a/etc/upgrade/shrink-cgm-table.in b/etc/upgrade/shrink-cgm-table.in
index cb15daf..546b32b 100644
--- a/etc/upgrade/shrink-cgm-table.in
+++ b/etc/upgrade/shrink-cgm-table.in
@@ -53,10 +53,8 @@ use warnings;
 use lib "@LOCAL_LIB_PATH@";
 use lib "@RT_LIB_PATH@";
 
-use RT -init;
-RT->Config->Set('LogToSTDERR' => 'debug');
-
-$| = 1;
+use RT::Interface::CLI qw(Init);
+Init();
 
 use RT::CachedGroupMembers;
 my $cgms = RT::CachedGroupMembers->new( RT->SystemUser );
diff --git a/etc/upgrade/shrink-transactions-table.in b/etc/upgrade/shrink-transactions-table.in
index be14cb3..5606d48 100644
--- a/etc/upgrade/shrink-transactions-table.in
+++ b/etc/upgrade/shrink-transactions-table.in
@@ -53,10 +53,8 @@ use warnings;
 use lib "@LOCAL_LIB_PATH@";
 use lib "@RT_LIB_PATH@";
 
-use RT -init;
-RT->Config->Set('LogToSTDERR' => 'debug');
-
-$| = 1;
+use RT::Interface::CLI qw(Init);
+Init();
 
 use RT::Transactions;
 my $txns = RT::Transactions->new( RT->SystemUser );
diff --git a/etc/upgrade/split-out-cf-categories.in b/etc/upgrade/split-out-cf-categories.in
index 12ce5ee..6b01063 100644
--- a/etc/upgrade/split-out-cf-categories.in
+++ b/etc/upgrade/split-out-cf-categories.in
@@ -53,10 +53,8 @@ use warnings;
 use lib "@LOCAL_LIB_PATH@";
 use lib "@RT_LIB_PATH@";
 
-use RT -init;
-RT->Config->Set('LogToSTDERR' => 'debug');
-
-$| = 1;
+use RT::Interface::CLI qw(Init);
+Init();
 
 $RT::Handle->BeginTransaction();
 
diff --git a/etc/upgrade/switch-templates-to.in b/etc/upgrade/switch-templates-to.in
index f41ff68..fed2d85 100644
--- a/etc/upgrade/switch-templates-to.in
+++ b/etc/upgrade/switch-templates-to.in
@@ -53,10 +53,8 @@ use warnings;
 use lib "@LOCAL_LIB_PATH@";
 use lib "@RT_LIB_PATH@";
 
-use RT -init;
-RT->Config->Set('LogToSTDERR' => 'info');
-
-$| = 1;
+use RT::Interface::CLI qw(Init);
+Init();
 
 my $to = shift || '';
 my $from;
diff --git a/etc/upgrade/time-worked-history.in b/etc/upgrade/time-worked-history.in
index 1596265..9eafd7b 100644
--- a/etc/upgrade/time-worked-history.in
+++ b/etc/upgrade/time-worked-history.in
@@ -53,10 +53,10 @@ use warnings;
 use lib "@LOCAL_LIB_PATH@";
 use lib "@RT_LIB_PATH@";
 
-use RT -init;
-RT->Config->Set('LogToSTDERR' => 'info');
+use RT::Interface::CLI qw(Init);
+Init();
 
-my $dbh = $RT::Handle->dbh;
+my $dbh = RT->DatabaseHandle->dbh;
 my $ids = $dbh->selectcol_arrayref(
     "SELECT t1.id FROM Tickets t1, Tickets t2 WHERE t1.id = t2.EffectiveId"
     ." AND t2.id != t2.EffectiveId AND t2.EffectiveId = t1.id"
diff --git a/etc/upgrade/upgrade-articles.in b/etc/upgrade/upgrade-articles.in
index 2619c11..c6f5486 100644
--- a/etc/upgrade/upgrade-articles.in
+++ b/etc/upgrade/upgrade-articles.in
@@ -53,11 +53,8 @@ use warnings;
 use lib "@LOCAL_LIB_PATH@";
 use lib "@RT_LIB_PATH@";
 
-use RT -init;
-
-RT->Config->Set('LogToSTDERR' => 'debug');
-
-$| = 1;
+use RT::Interface::CLI qw(Init);
+Init();
 
 my $db_name = RT->Config->Get('DatabaseName');
 my $db_type = RT->Config->Get('DatabaseType');
diff --git a/sbin/rt-attributes-viewer.in b/sbin/rt-attributes-viewer.in
index 646026e..f48df30 100644
--- a/sbin/rt-attributes-viewer.in
+++ b/sbin/rt-attributes-viewer.in
@@ -66,21 +66,11 @@ BEGIN { # BEGIN RT CMD BOILERPLATE
 
 }
 
-use Getopt::Long;
-my %opt;
-GetOptions( \%opt, 'help|h', );
+use RT::Interface::CLI qw(Init);
+Init();
 
 my $id = shift;
-
-if ( $opt{help} || !$id ) {
-    require Pod::Usage;
-    Pod::Usage::pod2usage({ verbose => 2 });
-    exit;
-}
-
-require RT;
-RT::LoadConfig();
-RT::Init();
+Pod::Usage::pod2usage({ verbose => 2 }) unless $id;
 
 require RT::Attribute;
 my $attr = RT::Attribute->new( RT->SystemUser );
diff --git a/sbin/rt-dump-metadata.in b/sbin/rt-dump-metadata.in
index c7c5a74..3b20998 100644
--- a/sbin/rt-dump-metadata.in
+++ b/sbin/rt-dump-metadata.in
@@ -70,26 +70,16 @@ BEGIN { # BEGIN RT CMD BOILERPLATE
 
 }
 
-use Getopt::Long;
+use RT::Interface::CLI qw(Init);
 my %opt;
-GetOptions( \%opt, "help|h",
+Init( \%opt,
     "limit-to-privileged|l",
     "skip-disabled|s",
     "all|a",
 );
 
-if ( $opt{help} ) {
-    require Pod::Usage;
-    Pod::Usage::pod2usage( { verbose => 2 } );
-    exit;
-}
-
-require RT;
 require XML::Simple;
 
-RT::LoadConfig();
-RT::Init();
-
 my %RV;
 my %Ignore = (
     All => [
diff --git a/sbin/rt-preferences-viewer.in b/sbin/rt-preferences-viewer.in
index ea39b76..19beba9 100644
--- a/sbin/rt-preferences-viewer.in
+++ b/sbin/rt-preferences-viewer.in
@@ -66,21 +66,9 @@ BEGIN { # BEGIN RT CMD BOILERPLATE
 
 }
 
-BEGIN {
-    use RT;
-    RT::LoadConfig();
-    RT::Init();
-};
-
-use Getopt::Long;
+use RT::Interface::CLI qw(Init);
 my %opt;
-GetOptions( \%opt, 'help|h', 'user|u=s', 'option|o=s' );
-
-if ( $opt{help} ) {
-    require Pod::Usage;
-    Pod::Usage::pod2usage({ verbose => 2 });
-    exit;
-}
+Init( \%opt, 'user|u=s', 'option|o=s' );
 
 require RT::Attributes;
 my $attrs = RT::Attributes->new( RT->SystemUser );
diff --git a/sbin/rt-session-viewer.in b/sbin/rt-session-viewer.in
index 9a8e913..0831c6e 100644
--- a/sbin/rt-session-viewer.in
+++ b/sbin/rt-session-viewer.in
@@ -66,21 +66,11 @@ BEGIN { # BEGIN RT CMD BOILERPLATE
 
 }
 
-use Getopt::Long;
-my %opt;
-GetOptions( \%opt, 'help|h', );
+use RT::Interface::CLI qw(Init);
+Init();
 
 my $session_id = shift;
-
-if ( $opt{help} || !$session_id ) {
-    require Pod::Usage;
-    Pod::Usage::pod2usage({ verbose => 2 });
-    exit;
-}
-
-require RT;
-RT::LoadConfig();
-RT::Init();
+Pod::Usage::pod2usage({ verbose => 2 }) unless $session_id;
 
 require RT::Interface::Web::Session;
 my %session;
diff --git a/sbin/rt-validator.in b/sbin/rt-validator.in
index 529f663..0ccbf11 100644
--- a/sbin/rt-validator.in
+++ b/sbin/rt-validator.in
@@ -66,23 +66,18 @@ BEGIN { # BEGIN RT CMD BOILERPLATE
 
 }
 
-use Getopt::Long;
+use RT::Interface::CLI qw(Init);
 my %opt = ();
-GetOptions(
+Init(
     \%opt,
     'check|c',
     'resolve',
     'force',
     'verbose|v',
-    'help|h',
     'links-only',
 );
 
-if ( $opt{help} || !$opt{check} ) {
-    require Pod::Usage;
-    print Pod::Usage::pod2usage( { verbose => 2 } );
-    exit 2;
-}
+Pod::Usage::pod2usage( { verbose => 2 } ) unless $opt{check};
 
 usage_warning() if $opt{'resolve'} && !$opt{'force'};
 
@@ -100,10 +95,6 @@ END
     <STDIN>;
 }
 
-use RT;
-RT::LoadConfig();
-RT::Init();
-
 my $dbh = $RT::Handle->dbh;
 my $db_type = RT->Config->Get('DatabaseType');
 

commit e522b3520f5fdc9c25798cfc84b678a6e807a906
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 19:45:49 2014 -0500

    Instead of explicitly printing to STDERR, use the logging infrastructure

diff --git a/etc/upgrade/shrink-cgm-table.in b/etc/upgrade/shrink-cgm-table.in
index 546b32b..da44b45 100644
--- a/etc/upgrade/shrink-cgm-table.in
+++ b/etc/upgrade/shrink-cgm-table.in
@@ -100,7 +100,7 @@ while ( my $rec = FetchNext( $cgms ) ) {
     $RT::Handle->BeginTransaction;
     my ($status) = $rec->Delete;
     unless ($status) {
-        print STDERR "Couldn't delete CGM #". $rec->id;
+        $RT::Logger->error( "Couldn't delete CGM #". $rec->id );
         exit 1;
     }
     $RT::Handle->Commit;
diff --git a/etc/upgrade/shrink-transactions-table.in b/etc/upgrade/shrink-transactions-table.in
index 5606d48..cec7baa 100644
--- a/etc/upgrade/shrink-transactions-table.in
+++ b/etc/upgrade/shrink-transactions-table.in
@@ -102,7 +102,7 @@ while ( my $rec = FetchNext( $txns ) ) {
     $RT::Handle->BeginTransaction;
     my ($status) = $rec->Delete;
     unless ($status) {
-        print STDERR "Couldn't delete TXN #". $rec->id;
+        $RT::Logger->error( "Couldn't delete TXN #". $rec->id );
         exit 1;
     }
     $RT::Handle->Commit;

commit 0242e29b7e6101e571dee3230f38c7ec2226076e
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 19:46:29 2014 -0500

    POD technically requires a blank line before each directive

diff --git a/sbin/rt-email-group-admin.in b/sbin/rt-email-group-admin.in
index 1b2ce5f..feaf051 100644
--- a/sbin/rt-email-group-admin.in
+++ b/sbin/rt-email-group-admin.in
@@ -46,6 +46,7 @@
 # those contributions and any derivatives thereof.
 #
 # END BPS TAGGED BLOCK }}}
+
 =head1 NAME
 
 rt-email-group-admin - Command line tool for administrating NotifyGroup actions
@@ -73,7 +74,7 @@ For example you can create the following action using this script:
     rt-email-group-admin --create 'Notify developers' --group 'Development Team'
 
 Then you can add the followoing scrip to your Bugs queue:
-    
+
     Condition: On Create
     Action:    Notify developers
     Template:  Transaction
diff --git a/sbin/rt-shredder.in b/sbin/rt-shredder.in
index 0a962fb..f861471 100644
--- a/sbin/rt-shredder.in
+++ b/sbin/rt-shredder.in
@@ -46,6 +46,7 @@
 # those contributions and any derivatives thereof.
 #
 # END BPS TAGGED BLOCK }}}
+
 =head1 NAME
 
 rt-shredder - Script which wipe out tickets from RT DB

commit 582d022c17b8ea16391547896995da4e658cf2c5
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Dec 3 23:07:20 2014 -0500

    Forward-port run_validator (and timeout functionality) from RT 4.0
    
    547af3b4 added a test to check that rt-validator didn't infinite loop;
    as such, it added timeout functionality to run_validator in RT::Test.
    run_validator had been extracted in 21c7ddf7; however, the _same_
    extraction was also done on 4.2-trunk in 80ae47db, but placed the
    function elsewhere in the file.  When 4.0-trunk was merged to 4.2-trunk
    in 1f4a0bc7, it (without a merge conflict!) placed a duplicate into
    RT::Test -- which was removed by hand before being committed.
    
    Unfortunately, the version that was removed was the 4.0-trunk version,
    which had the timeout feature added in 547af3b4.  As such, 4.2-trunk's
    test has since been passing --timeout=30 to rt-validator, instead of
    setting a 30-second alarm.  Because rt-validator did not validate its
    command-line arguments, and the code did _not_ infinite-loop, this went
    unnoticed.
    
    9d354ba switched rt-validator to use RT::Interface::CLI::Init(), which
    does error if command-line argument parsing failed, thus exiting with a
    non-zero exit status, failing the test and making this situation clear.
    
    Forward-port the run_validator from 4.0-trunk, giving it the
    functionality intended to be added in 547af3b4 by way of 1f4a0bc7.

diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index aa03a9b..bdeb001 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -1273,14 +1273,16 @@ sub fetch_caught_mails {
 sub clean_caught_mails {
     unlink $tmp{'mailbox'};
 }
-my $validator_path = "$RT::SbinPath/rt-validator";
+
 sub run_validator {
     my $self = shift;
-    my %args = (check => 1, resolve => 0, force => 1, @_ );
+    my %args = (check => 1, resolve => 0, force => 1, timeout => 0, @_ );
 
-    my $cmd = $validator_path;
+    my $cmd = "$RT::SbinPath/rt-validator";
     die "Couldn't find $cmd command" unless -f $cmd;
 
+    my $timeout = delete $args{timeout};
+
     while( my ($k,$v) = each %args ) {
         next unless $v;
         $cmd .= " --$k '$v'";
@@ -1292,9 +1294,14 @@ sub run_validator {
     my $pid = IPC::Open2::open2($child_out, $child_in, $cmd);
     close $child_in;
 
-    my $result = do { local $/; <$child_out> };
+    local $SIG{ALRM} = sub { kill KILL => $pid; die "Timeout!" };
+
+    alarm $timeout if $timeout;
+    my $result = eval { local $/; <$child_out> };
+    warn $@ if $@;
     close $child_out;
     waitpid $pid, 0;
+    alarm 0;
 
     DBIx::SearchBuilder::Record::Cachable->FlushCache
         if $args{'resolve'};

-----------------------------------------------------------------------


More information about the rt-commit mailing list