Removed /usr/local from CDPATH
[clearscm.git] / cq / cqinfo.pl
1 #!/usr/bin/env cqperl
2 use strict;
3 use warnings;
4
5 =pod
6
7 =pod
8
9 =head1 NAME $RCSfile: cqinfo.pl,v $
10
11 Clearquest Info
12
13 This script takes some parameters and gets information from a Clearquest
14 database.
15
16 =head1 VERSION
17
18 =over
19
20 =item Author
21
22 Andrew DeFaria <Andrew@ClearSCM.com>
23
24 =item Revision
25
26 $Revision: 1.6 $
27
28 =item Created:
29
30 Mon Jul 30 12:05:45 PDT 2012
31
32 =item Modified:
33
34 $Date: 2013/03/15 00:19:36 $
35
36 =back
37
38 =head1 SYNOPSIS
39
40  Usage: cqinfo.pl [-u|sage] [-v|erbose] [-d|ebug]
41                   [-username <username>] [-password <password>]
42                   [-database <dbname>] [-dbset <dbset>]
43                   [-record <record>] [-key <key>]
44                   [-fields <field1>,<field2>,...]
45                   [-module] [-server <server>] [-port <port>]
46                   
47  Where:
48    -u|sage:     Displays usage
49    -v|erbose:   Be verbose
50    -de|bug:     Output debug messages
51
52    -r|ecord:    Record to interrogate (Default: Defect)
53    -k|ey:       Key to locate the record with (Note that if you supply
54                 simply a number (e.g. 1234) then we will expand that with
55                 leading zeroes to the length of 8 digits and prepend the
56                 database name)
57    -f|ields:    List of fields to display (Default: All fields)
58                 
59    -use|rname:  Username to open database with (Default: from config file) 
60    -p|assword:  Password to open database with (Default: from config file) 
61    -da|tabase:  Database to open (Default: from config file)
62    -db|set:     Database Set to use (Default: from config file)
63    -m|odule:    Type of Clearquest module to use. Must be one of 'api', 
64                 'client', or 'rest'. The 'api' module can only be used if
65                 Clearquest is installed locally. The 'client' module can
66                 only be successful if a corresponding server is running. And
67                 the 'rest' module can only be used if a CQ Web server has
68                 been set up and configured (Default: rest)
69    -s|erver:    For module = client or rest this is the name of the server 
70                 that will be providing the service
71    -p|ort:      For module = client, this is the point on the server to talk
72                 through.
73
74 =head1 Options
75
76 Options are keep in the cq.conf file in etc. They specify the default options
77 listed below. Or you can export the option name to the env(1) to override the
78 defaults in cq.conf. Finally you can programmatically set the options when you
79 call new by passing in a %parms hash. To specify the %parms hash key remove the
80 CQ_ portion and lc the rest.
81
82 =for html <blockquote>
83
84 =over
85
86 =item CQ_WEBHOST
87
88 The web host to contact with leading http://
89
90 =item CQ_DATABASE
91
92 Name of database to connect to (Default: from config file)
93
94 =item CQ_USERNAME
95
96 User name to connect as (Default: from config file)
97
98 =item CQ_PASSWORD
99
100 Password for CQ_USERNAME
101
102 =item CQ_DBSET
103
104 Database Set name (Default: from config file)
105
106 =item CQ_SERVER
107
108 Clearquest::Server name to connect to (Default: from config file)
109
110 =item CQ_PORT
111
112 Clearquest::Server port to connect to (Default: from config file)
113
114 =back
115
116 =cut
117
118 use FindBin;
119 use Getopt::Long;
120
121 use lib "$FindBin::Bin/../lib";
122
123 use Clearquest;
124 use Display;
125 use Utils;
126
127 my %opts;
128
129 GetOptions (
130   \%opts,
131   usage   => sub { Usage },
132   verbose => sub { set_verbose },
133   debug   => sub { set_debug },
134   'module=s',
135   'username=s',
136   'password=s',
137   'database=s',
138   'dbset=s',
139   'record=s',
140   'key=s',
141   'server=s',
142   'port=i',
143   'fields=s@',
144 ) || Usage;
145
146 Usage "You must specify -key" unless $opts{key};
147
148 $opts{module} = lc $opts{module} if $opts{module};
149
150 # Default to Defect
151 my $record = delete $opts{record} || 'Defect';
152 my $key    = delete $opts{key};
153 my @fields;
154
155 if ($opts{fields}) {
156   push @fields, split /\s*,\s*/ foreach (@{$opts{fields}});
157 } # if
158
159 # Translate any options to ones that the lib understands
160 $opts{CQ_USERNAME} = delete $opts{username};
161 $opts{CQ_PASSWORD} = delete $opts{password};
162 $opts{CQ_DATABASE} = delete $opts{database};
163 $opts{CQ_DBSET}    = delete $opts{dbset};
164 $opts{CQ_SERVER}   = delete $opts{server};
165 $opts{CQ_PORT}     = delete $opts{port};
166
167 my $cq;
168
169 my $module = delete $opts{module};
170
171 $cq = Clearquest->new (%opts);
172
173 $cq->connect;
174
175 # Fix key if necessary
176 if ($key =~ /^(\d+)$/) {
177   $key = $cq->{database} . 0 x (8 - length $1) . $1;
178 } # if 
179
180 my %record = $cq->get ($record, $key, @fields);
181
182 unless ($cq->error) {
183   foreach my $field (sort keys %record) {
184     if (ref $record{$field} eq 'ARRAY') {
185       display "$field (LIST):";
186       
187       display "\t$_" foreach (@{$record{$field}});
188     } else {
189       display_nolf "$field: ";
190       
191       if ($record{$field}) {
192         display $record{$field};
193       } else {
194         display '<undef>';
195       } # if
196     } # if
197   } # foreach
198
199   exit 0;
200 } else {
201   error "Unable to get $record with key $key\n" . $cq->errmsg, $cq->error;
202 } # unless