aboutsummaryrefslogtreecommitdiff
blob: 7c174806440d4cf27e2fb7b3473041065eecd2ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/perl
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

use warnings;

use File::Basename;
BEGIN { chdir dirname($0) . "/.."; }
use lib qw(. lib);

use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Util;
use Bugzilla::Bug;

use Term::ReadLine;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Terse    = 1;
$Data::Dumper::Indent   = 1;
$Data::Dumper::Useqq    = 1;
$Data::Dumper::Maxdepth = 1;
$Data::Dumper::Deparse  = 0;

my $sysname = get_text('term', {term => 'Bugzilla'});
my $term    = new Term::ReadLine "$sysname Console";
read_history($term);
END { write_history($term) }

while (defined(my $input = $term->readline("$sysname> "))) {
  my @res = eval($input);
  if ($@) {
    warn $@;
  }
  else {
    print Dumper(@res);
  }
}
print STDERR "\n";
exit 0;

# d: full dump (normal behavior is limited to depth of 1)
sub d {
  local $Data::Dumper::Maxdepth = 0;
  local $Data::Dumper::Deparse  = 1;
  print Dumper(@_);
  return ();
}

# p: print as a single string (normal behavior puts list items on separate lines)
sub p {
  no warnings;    # suppress possible undefined var message
  print(@_, "\n");
  return ();
}

sub filter {
  my $name = shift;
  my $filter
    = Bugzilla->template->{SERVICE}->{CONTEXT}->{CONFIG}->{FILTERS}->{$name};
  if (scalar @_) {
    return $filter->(@_);
  }
  else {
    return $filter;
  }
}

sub b { get_object('Bugzilla::Bug',   @_) }
sub u { get_object('Bugzilla::User',  @_) }
sub f { get_object('Bugzilla::Field', @_) }

sub get_object {
  my $class = shift;
  $_ = shift;
  my @results = ();

  if (ref $_ eq 'HASH' && keys %$_) {
    @results = @{$class->match($_)};
  }
  elsif (m/^\d+$/) {
    @results = ($class->new($_));
  }
  elsif (m/\w/i && grep { $_ eq 'name' } ($class->_get_db_columns)) {
    @results = @{$class->match({name => $_})};
  }
  else {
    @results = ();
  }

  if (wantarray) {
    return @results;
  }
  else {
    return shift @results;
  }
}

sub read_history {
  my ($term) = @_;

  if (open HIST, "<$ENV{HOME}/.bugzilla_console_history") {
    foreach (<HIST>) {
      chomp;
      $term->addhistory($_);
    }
    close HIST;
  }
}

sub write_history {
  my ($term) = @_;

  if (
    $term->can('GetHistory') && open HIST,
    ">$ENV{HOME}/.bugzilla_console_history"
    )
  {
    my %seen_hist = ();
    my @hist      = ();
    foreach my $line (reverse $term->GetHistory()) {
      next unless $line =~ m/\S/;
      next if $seen_hist{$line};
      $seen_hist{$line} = 1;
      push @hist, $line;
      last if (scalar @hist > 500);
    }
    foreach (reverse @hist) {
      print HIST $_, "\n";
    }
    close HIST;
  }
}

__END__

=head1 NAME

B<console.pl> - command-line interface to Bugzilla API

=head1 SYNOPSIS

$ B<contrib/console.pl>

Bugzilla> B<b(5)-E<gt>short_desc>

=over 8

"Misplaced Widget"

=back

Bugzilla> B<$f = f "cf_subsystem"; scalar @{$f-E<gt>legal_values}>

=over 8

177

=back

Bugzilla> B<p filter html_light, "1 E<lt> 2 E<lt>bE<gt>3E<lt>/bE<gt>">

=over 8

1 &lt; 2 E<lt>bE<gt>3E<lt>/bE<gt>

=back

Bugzilla> B<$u = u 5; $u-E<gt>groups; d $u>

=head1 DESCRIPTION

Loads Bugzilla packages and prints expressions with Data::Dumper.
Useful for checking results of Bugzilla API calls without opening
a debug file from a cgi.