1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Interface::CLI;
56 our @EXPORT_OK = qw(CleanEnv GetCurrentUser debug loc Init);
60 RT::Interface::CLI - helper functions for creating a commandline RT interface
64 use lib "/opt/rt4/local/lib", "/opt/rt4/lib";
66 use RT::Interface::CLI qw(GetCurrentUser Init loc);
68 # Process command-line arguments, load the configuration, and connect
72 # Get the current user all loaded
73 my $CurrentUser = GetCurrentUser();
75 print loc('Hello!'); # Synonym of $CurrentUser->loc('Hello!');
88 Removes some of the nastiest nasties from the user's environment.
93 RT->Deprecated( Remove => "4.4" );
95 $ENV{'PATH'} = '/bin:/usr/bin'; # or whatever you need
96 $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
97 $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
98 $ENV{'ENV'} = '' if defined $ENV{'ENV'};
99 $ENV{'IFS'} = '' if defined $ENV{'IFS'};
107 my $CurrentUser; # shared betwen GetCurrentUser and loc
110 =head2 GetCurrentUser
112 Figures out the uid of the current user and returns an RT::CurrentUser object
113 loaded with that user. if the current user isn't found, returns a copy of RT::Nobody.
119 require RT::CurrentUser;
121 #Instantiate a user object
123 my $Gecos= (getpwuid($<))[0];
125 #If the current user is 0, then RT will assume that the User object
126 #is that of the currentuser.
128 $CurrentUser = RT::CurrentUser->new();
129 $CurrentUser->LoadByGecos($Gecos);
131 unless ($CurrentUser->Id) {
132 $RT::Logger->error("No user with a GECOS (unix login) of '$Gecos' was found.");
135 return($CurrentUser);
140 Synonym of $CurrentUser->loc().
145 die "No current user yet" unless $CurrentUser ||= RT::CurrentUser->new;
146 return $CurrentUser->loc(@_);
152 RT->Deprecated( Remove => "4.4", Instead => '$RT::Logger->debug' );
153 $RT::Logger->debug(@_);
160 Pod::Usage::pod2usage(
161 -message => $args{'Message'},
162 -exitval => $args{'ExitValue'} || 0,
164 -sections => $args{'Sections'} || ($args{'ExitValue'}
166 : 'NAME|USAGE|OPTIONS|DESCRIPTION'
173 A shim for L<Getopt::Long/GetOptions> which automatically adds a
174 C<--help> option if it is not supplied. It then calls L<RT/LoadConfig>
177 It sets the C<LogToSTDERR> setting to C<warning>, to ensure that the
178 user sees all relevant warnings. It also adds C<--quiet> and
179 C<--verbose> options, which adjust the C<LogToSTDERR> value to C<error>
180 or C<debug>, respectively.
185 require Getopt::Long;
196 push @args, $_ => \($hash->{$1});
208 push @args, "help|h!" => \($hash->{help})
209 unless $exists{help};
211 push @args, "verbose|v!" => \($hash->{verbose})
212 unless $exists{verbose};
214 push @args, "quiet|q!" => \($hash->{quiet})
215 unless $exists{quiet};
217 my $ok = Getopt::Long::GetOptions( @args );
218 Pod::Usage::pod2usage(1) if not $ok and not defined wantarray;
222 Pod::Usage::pod2usage({ verbose => 2})
223 if not $exists{help} and $hash->{help};
228 if (not $exists{quiet} and $hash->{quiet}) {
229 RT->Config->Set(LogToSTDERR => "error");
230 } elsif (not $exists{verbose} and $hash->{verbose}) {
231 RT->Config->Set(LogToSTDERR => "debug");
233 RT->Config->Set(LogToSTDERR => "warning");
243 RT::Base->_ImportOverlays();