rt 4.2.16
[freeside.git] / rt / lib / RT / Interface / CLI.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2019 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
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
16 # from www.gnu.org.
17 #
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.
22 #
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.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
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.)
37 #
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.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 package RT::Interface::CLI;
50 use strict;
51 use warnings;
52
53 use RT::Base;
54
55 use base 'Exporter';
56 our @EXPORT_OK = qw(CleanEnv GetCurrentUser debug loc Init);
57
58 =head1 NAME
59
60   RT::Interface::CLI - helper functions for creating a commandline RT interface
61
62 =head1 SYNOPSIS
63
64   use lib "/opt/rt4/local/lib", "/opt/rt4/lib";
65
66   use RT::Interface::CLI  qw(GetCurrentUser Init loc);
67
68   # Process command-line arguments, load the configuration, and connect
69   # to the database
70   Init();
71
72   # Get the current user all loaded
73   my $CurrentUser = GetCurrentUser();
74
75   print loc('Hello!'); # Synonym of $CurrentUser->loc('Hello!');
76
77 =head1 DESCRIPTION
78
79
80 =head1 METHODS
81
82
83 =cut
84
85
86 =head2 CleanEnv
87
88 Removes some of the nastiest nasties from the user's environment.
89
90 =cut
91
92 sub CleanEnv {
93     RT->Deprecated( Remove => "4.4" );
94
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'};
100 }
101
102
103
104
105 {
106
107     my $CurrentUser; # shared betwen GetCurrentUser and loc
108
109
110 =head2 GetCurrentUser
111
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.
114
115 =cut
116
117 sub GetCurrentUser  {
118
119     require RT::CurrentUser;
120
121     #Instantiate a user object
122
123     my $Gecos= (getpwuid($<))[0];
124
125     #If the current user is 0, then RT will assume that the User object
126     #is that of the currentuser.
127
128     $CurrentUser = RT::CurrentUser->new();
129     $CurrentUser->LoadByGecos($Gecos);
130
131     unless ($CurrentUser->Id) {
132         $RT::Logger->error("No user with a GECOS (unix login) of '$Gecos' was found.");
133     }
134
135     return($CurrentUser);
136 }
137
138 =head2 loc
139
140   Synonym of $CurrentUser->loc().
141
142 =cut
143
144 sub loc {
145     die "No current user yet" unless $CurrentUser ||= RT::CurrentUser->new;
146     return $CurrentUser->loc(@_);
147 }
148
149 }
150
151 sub debug {
152     RT->Deprecated( Remove => "4.4", Instead => '$RT::Logger->debug' );
153     $RT::Logger->debug(@_);
154 }
155
156 sub ShowHelp {
157     my $self = shift;
158     my %args = @_;
159     require Pod::Usage;
160     Pod::Usage::pod2usage(
161         -message => $args{'Message'},
162         -exitval => $args{'ExitValue'} || 0, 
163         -verbose => 99,
164         -sections => $args{'Sections'} || ($args{'ExitValue'}
165             ? 'NAME|USAGE'
166             : 'NAME|USAGE|OPTIONS|DESCRIPTION'
167         ),
168     );
169 }
170
171 =head2 Init
172
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>
175 and L<RT/Init>.
176
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.
181
182 =cut
183
184 sub Init {
185     require Getopt::Long;
186     require Pod::Usage;
187
188     my %exists;
189     my @args;
190     my $hash;
191     if (ref $_[0]) {
192         $hash = shift(@_);
193         for (@_) {
194             m/^([a-zA-Z0-9-]+)/;
195             $exists{$1}++;
196             push @args, $_ => \($hash->{$1});
197         }
198     } else {
199         $hash = {};
200         @args = @_;
201         while (@_) {
202             my $key = shift(@_);
203             $exists{$key}++;
204             shift(@_);
205         }
206     }
207
208     push @args, "help|h!" => \($hash->{help})
209         unless $exists{help};
210
211     push @args, "verbose|v!" => \($hash->{verbose})
212         unless $exists{verbose};
213
214     push @args, "quiet|q!" => \($hash->{quiet})
215         unless $exists{quiet};
216
217     my $ok = Getopt::Long::GetOptions( @args );
218     Pod::Usage::pod2usage(1) if not $ok and not defined wantarray;
219
220     return unless $ok;
221
222     Pod::Usage::pod2usage({ verbose => 2})
223           if not $exists{help} and $hash->{help};
224
225     require RT;
226     RT::LoadConfig();
227
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");
232     } else {
233         RT->Config->Set(LogToSTDERR => "warning");
234     }
235
236     RT::Init();
237
238     $| = 1;
239
240     return $ok;
241 }
242
243 RT::Base->_ImportOverlays();
244
245 1;