Removed /usr/local from CDPATH
[clearscm.git] / cq / cqaction.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 =pod
6
7 =pod
8
9 =head1 NAME $RCSfile: cqaction.pl,v $
10
11 Clearquest Action
12
13 This script attempt to apply an action to a statefull Clearquest record. 
14
15 =head1 VERSION
16
17 =over
18
19 =item Author
20
21 Andrew DeFaria <Andrew@ClearSCM.com>
22
23 =item Revision
24
25 $Revision: 2.2 $
26
27 =item Created:
28
29 Mon Jul 30 12:05:45 PDT 2012
30
31 =item Modified:
32
33 $Date: 2012/12/18 19:44:10 $
34
35 =back
36
37 =head1 SYNOPSIS
38
39  Usage: cqaction.pl [-u|sage] [-v|erbose] [-d|ebug]
40                     [-username <username>] [-password <password>]
41                     [-database <dbname>] [-dbset <dbset>]
42                     [-record <record>] [-key <key>]
43                     [-action <action>]
44                     [-module] [-server <server>] [-port <port>]
45                          
46                   
47  Where:
48    -u|sage:     Displays usage
49    -v|erbose:   Be verbose
50    -de|bug:     Output debug messages
51
52    -record:     Record to apply the action to (Default: Defect)
53    -key:        Key to locate the record with (Note that if you supply simply
54                 a number (e.g. 1234) then we will expand that with leading
55                 zeroes to the length of 8 digits and prepend the database name)   
56    -action:     Action to apply (Default: Modify)
57
58    -use|rname:  Username to open database with (Default: from config file) 
59    -p|assword:  Password to open database with (Default: from config file) 
60    -da|tabase:  Database to open (Default: from config file)
61    -db|set:     Database Set to use (Default: from config file)
62    -m|odule:    Type of Clearquest module to use. Must be one of 'api', 
63                 'client', or 'rest'. The 'api' module can only be used if
64                 Clearquest is installed locally. The 'client' module can
65                 only be successful if a corresponding server is running. And
66                 the 'rest' module can only be used if a CQ Web server has
67                 been set up and configured (Default: rest)
68    -s|erver:    For module = client or rest this is the name of the server 
69                 that will be providing the service
70    -p|ort:      For module = client, this is the point on the server to talk
71                 through.
72
73 =head1 Options
74
75 Options are keep in the cq.conf file in etc. They specify the default options
76 listed below. Or you can export the option name to the env(1) to override the
77 defaults in cq.conf. Finally you can programmatically set the options when you
78 call new by passing in a %parms hash. To specify the %parms hash key remove the
79 CQ_ portion and lc the rest.
80
81 =for html <blockquote>
82
83 =over
84
85 =item CQ_WEBHOST
86
87 The web host to contact with leading http://
88
89 =item CQ_DATABASE
90
91 Name of database to connect to (Default: from config file)
92
93 =item CQ_USERNAME
94
95 User name to connect as (Default: from config file)
96
97 =item CQ_PASSWORD
98
99 Password for CQ_USERNAME
100
101 =item CQ_DBSET
102
103 Database Set name (Default: from config file)
104
105 =item CQ_SERVER
106
107 Clearquest::Server name to connect to (Default: from config file)
108
109 =item CQ_PORT
110
111 Clearquest::Server port to connect to (Default: from config file)
112
113 =back
114
115 =head1 Modifying fields while changing state
116
117 If you need to modify fields while changing state then feed them to this 
118 script's stdin in the form of:
119
120  <field>=<value>
121
122 B<Note:> Don't forget that you will be prompted field=value and you'll need
123 to signal that you have entered all of the field/value pairs you intended with
124 Ctrl-D (or Ctrl-Z on Windows). You can short circut this by feeding something
125 like /dev/null to stdin like so:
126
127   $ cat /dev/null > cqaction.pl <parms>
128  
129 =cut
130
131 use FindBin;
132 use Getopt::Long;
133
134 use lib "$FindBin::Bin/../lib";
135
136 use Display;
137 use Utils;
138
139 my %opts;
140
141 sub getFields () {
142   my %values;
143   
144   verbose "Enter <field>=<value> pairs and Ctrl-D to end input";
145   
146   while (<STDIN>) {
147     if (/^(\s+)(=|:)(\.*)/) {
148       $values{$1} = $2;
149     } # if
150   } # while
151   
152   verbose "All <field>=<value> pairs accepted";
153   
154   return %values;
155 } # getFields
156
157 $opts{module} = 'rest';
158
159 GetOptions (
160   \%opts,
161   usage   => sub { Usage },
162   verbose => sub { set_verbose },
163   debug   => sub { set_debug },
164   'module=s',
165   'username=s',
166   'password=s',
167   'database=s',
168   'dbset=s',
169   'record=s',
170   'key=s',
171   'server=s',
172   'port=i',
173   'action=s',
174 ) || Usage;
175
176 Usage "You must specify -key" unless $opts{key};
177
178 # Default to Defect
179 my $record = delete $opts{record} || 'Defect';
180 my $key    = delete $opts{key};
181 my $action = delete $opts{action} || 'Modify';
182
183 # Translate any options to ones that the lib understands
184 $opts{CQ_USERNAME} = delete $opts{username};
185 $opts{CQ_PASSWORD} = delete $opts{password};
186 $opts{CQ_DATABASE} = delete $opts{database};
187 $opts{CQ_DBSET}    = delete $opts{dbset};
188 $opts{CQ_SERVER}   = delete $opts{server};
189 $opts{CQ_PORT}     = delete $opts{port};
190
191 my $cq;
192
193 my $module = lc delete $opts{module};
194
195 if ($module eq 'rest') {
196   require Clearquest::REST;
197   
198   $cq = Clearquest::REST->new (%opts);
199 } elsif ($module eq 'client') {
200   require Clearquest::Client;
201   
202   $cq = Clearquest::Client->new (%opts);
203   
204   $cq->connect;
205 } elsif ($module eq 'api') {
206   require Clearquest;
207   
208   $cq = Clearquest->new (%opts);
209
210   $cq->connect;
211 } else {
212   Usage "Invalid module - $opts{module}";
213 } # if
214
215 # Fix key if necessary
216 if ($key =~ /^(\d+)$/) {
217   $key = $cq->{database} . 0 x (8 - length $1) . $1;
218 } # if 
219
220 my %values = getFields;
221
222 my $errmsg = $cq->modify ($record, $key, $action, %values);
223
224 unless ($cq->cqerror) {
225   verbose "Successfully applied $action to $record:$key";
226   
227   exit 0;
228 } else {
229   error "Unable to apply $action to $record:$key\n" . $cq->cqerrmsg, $cq->cqerror;
230 } # unless