summaryrefslogtreecommitdiff
path: root/lib/App/Sqitch/Command/plan.pm
blob: d8f20ad94c03a409d5b786a322b5ac38fda71dd1 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package App::Sqitch::Command::plan;

use 5.010;
use strict;
use warnings;
use utf8;
use Locale::TextDomain qw(App-Sqitch);
use App::Sqitch::X qw(hurl);
use Moo;
use Types::Standard qw(Str Int ArrayRef Bool);
use Type::Utils qw(class_type);
use App::Sqitch::ItemFormatter;
use namespace::autoclean;
use Try::Tiny;
extends 'App::Sqitch::Command';

our $VERSION = 'v1.4.1'; # VERSION

my %FORMATS;
$FORMATS{raw} = <<EOF;
%{:event}C%e %H%{reset}C%T
name      %n
project   %o
%{requires}a%{conflicts}aplanner   %{name}p <%{email}p>
planned   %{date:raw}p

%{    }B
EOF

$FORMATS{full} = <<EOF;
%{:event}C%L %h%{reset}C%T
%{name}_ %n
%{project}_ %o
%R%X%{planner}_ %p
%{planned}_ %{date}p

%{    }B
EOF

$FORMATS{long} = <<EOF;
%{:event}C%L %h%{reset}C%T
%{name}_ %n
%{project}_ %o
%{planner}_ %p

%{    }B
EOF

$FORMATS{medium} = <<EOF;
%{:event}C%L %h%{reset}C
%{name}_ %n
%{planner}_ %p
%{date}_ %{date}p

%{    }B
EOF

$FORMATS{short} = <<EOF;
%{:event}C%L %h%{reset}C
%{name}_ %n
%{planner}_ %p

%{    }s
EOF

$FORMATS{oneline} = '%{:event}C%h %l%{reset}C %n%{cyan}C%t%{reset}C';

has target => (
    is      => 'ro',
    isa     => Str,
);

has event => (
    is      => 'ro',
    isa     => Str,
);

has change_pattern => (
    is      => 'ro',
    isa     => Str,
);

has planner_pattern => (
    is      => 'ro',
    isa     => Str,
);

has max_count => (
    is      => 'ro',
    isa     => Int,
);

has skip => (
    is      => 'ro',
    isa     => Int,
);

has reverse => (
    is      => 'ro',
    isa     => Bool,
    default => 0,
);

has headers => (
    is      => 'ro',
    isa     => Bool,
    default => 1,
);

has format => (
    is       => 'ro',
    isa      => Str,
    default  => $FORMATS{medium},
);

has formatter => (
    is       => 'ro',
    isa      => class_type('App::Sqitch::ItemFormatter'),
    lazy     => 1,
    default  => sub { App::Sqitch::ItemFormatter->new },
);

sub options {
    return qw(
        event=s
        target|t=s
        change-pattern|change=s
        planner-pattern|planner=s
        format|f=s
        date-format|date=s
        max-count|n=i
        skip=i
        reverse!
        color=s
        no-color
        abbrev=i
        oneline
        headers!
    );
}

sub configure {
    my ( $class, $config, $opt ) = @_;

    # Set base values if --oneline.
    if ($opt->{oneline}) {
        $opt->{format} ||= 'oneline';
        $opt->{abbrev} //= 6;
    }

    # Determine and validate the date format.
    my $date_format = delete $opt->{date_format} || $config->get(
        key => 'plan.date_format'
    );
    if ($date_format) {
        require App::Sqitch::DateTime;
        App::Sqitch::DateTime->validate_as_string_format($date_format);
    } else {
        $date_format = 'iso';
    }

    # Make sure the plan format is valid.
    if (my $format = $opt->{format}
        || $config->get(key => 'plan.format')
    ) {
        if ($format =~ s/^format://) {
            $opt->{format} = $format;
        } else {
            $opt->{format} = $FORMATS{$format} or hurl plan => __x(
                'Unknown plan format "{format}"',
                format => $format
            );
        }
    }

    # Determine how to handle ANSI colors.
    my $color = delete $opt->{no_color} ? 'never'
        : delete $opt->{color} || $config->get(key => 'plan.color');

    $opt->{formatter} = App::Sqitch::ItemFormatter->new(
        ( $date_format   ? ( date_format => $date_format          ) : () ),
        ( $color         ? ( color       => $color                ) : () ),
        ( $opt->{abbrev} ? ( abbrev      => delete $opt->{abbrev} ) : () ),
    );

    return $class->SUPER::configure( $config, $opt );
}

sub execute {
    my $self = shift;
    my ($targets) = $self->parse_args(
        target => $self->target,
        args   => \@_,
    );

    # Warn on multiple targets.
    my $target = shift @{ $targets };
    $self->warn(__x(
        'Too many targets specified; using {target}',
        target => $target->name,
    )) if @{ $targets };
    my $plan = $target->plan;

    # Exit with status 1 on no changes, probably not expected.
    hurl {
        ident   => 'plan',
        exitval => 1,
        message => __x(
            'No changes in {file}',
            file => $plan->file,
        ),
    } unless $plan->count;

    # Search the changes.
    my $iter = $plan->search_changes(
        operation => $self->event,
        name      => $self->change_pattern,
        planner   => $self->planner_pattern,
        limit     => $self->max_count,
        offset    => $self->skip,
        direction => $self->reverse ? 'DESC' : 'ASC',
    );

    # Send the results.
    my $formatter = $self->formatter;
    my $format    = $self->format;
    if ($self->headers) {
        $self->page( '# ', __x 'Project: {project}', project => $plan->project );
        $self->page( '# ', __x 'File:    {file}', file => $plan->file );
        $self->page('');
    }
    while ( my $change = $iter->() ) {
        $self->page( $formatter->format( $format, {
            event         => $change->is_deploy ? 'deploy' : 'revert',
            project       => $change->project,
            change_id     => $change->id,
            change        => $change->name,
            note          => $change->note,
            deploy_file   => $change->deploy_file,
            tags          => [ map { $_->format_name } $change->tags ],
            requires      => [ map { $_->as_string } $change->requires ],
            conflicts     => [ map { $_->as_string } $change->conflicts ],
            planned_at    => $change->timestamp,
            planner_name  => $change->planner_name,
            planner_email => $change->planner_email,
        } ) );
    }

    return $self;
}

1;

__END__

=head1 Name

App::Sqitch::Command::plan - List the changes in the plan

=head1 Synopsis

  my $cmd = App::Sqitch::Command::plan->new(%params);
  $cmd->execute;

=head1 Description

If you want to know how to use the C<plan> command, you probably want to be
reading C<sqitch-plan>. But if you really want to know how the C<plan> command
works, read on.

=head1 Interface

=head2 Attributes

=head3 C<change_pattern>

Regular expression to match against change names.

=head3 C<planner_pattern>

Regular expression to match against planner names.

=head3 C<event>

Event type buy which to filter entries to display.

=head3 C<format>

Display format template.

=head3 C<max_count>

Maximum number of entries to display.

=head3 C<reverse>

Reverse the usual order of the display of entries.

=head3 C<headers>

Output headers. Defaults to true.

=head3 C<skip>

Number of entries to skip before displaying entries.

=head2 Instance Methods

=head3 C<execute>

  $plan->execute;

Executes the plan command. The plan will be searched and the results output.

=head1 See Also

=over

=item L<sqitch-plan>

Documentation for the C<plan> command to the Sqitch command-line client.

=item L<sqitch>

The Sqitch command-line client.

=back

=head1 Author

David E. Wheeler <david@justatheory.com>

=head1 License

Copyright (c) 2012-2024 iovation Inc., David E. Wheeler

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=cut