File Coverage

blib/lib/App/Sqitch/Role/RevertDeployCommand.pm
Criterion Covered Total %
statement 44 44 100.0
branch n/a
condition n/a
subroutine 12 12 100.0
pod n/a
total 56 56 100.0


line stmt bran cond sub pod time code
1             package App::Sqitch::Role::RevertDeployCommand;
2              
3 3     3   2016 use 5.010;
  3         10  
4 3     3   21 use strict;
  3         6  
  3         82  
5 3     3   21 use warnings;
  3         6  
  3         98  
6 3     3   19 use utf8;
  3         9  
  3         15  
7 3     3   78 use Moo::Role;
  3         10  
  3         37  
8 3     3   1308 use App::Sqitch::Types qw(Str Int Bool HashRef);
  3         23  
  3         28  
9 3     3   3378 use Type::Utils qw(enum);
  3         9  
  3         22  
10 3     3   1677 use namespace::autoclean;
  3         15  
  3         25  
11 3     3   182 use Locale::TextDomain qw(App-Sqitch);
  3         15  
  3         20  
12 3     3   463 use App::Sqitch::X qw(hurl);
  3         7  
  3         22  
13              
14             requires 'sqitch';
15             requires 'command';
16             requires 'options';
17             requires 'configure';
18              
19             with 'App::Sqitch::Role::ContextCommand';
20             with 'App::Sqitch::Role::ConnectingCommand';
21              
22             our $VERSION = 'v1.4.0'; # VERSION
23              
24             has target => (
25             is => 'ro',
26             isa => Str,
27             );
28              
29             has verify => (
30             is => 'ro',
31             isa => Bool,
32             default => 0,
33             );
34              
35             has log_only => (
36             is => 'ro',
37             isa => Bool,
38             default => 0,
39             );
40              
41             has lock_timeout => (
42             is => 'ro',
43             isa => Int,
44             lazy => 1,
45             default => sub { App::Sqitch::Engine::default_lock_timeout() },
46             );
47              
48             has no_prompt => (
49             is => 'ro',
50             isa => Bool
51             );
52              
53             has prompt_accept => (
54             is => 'ro',
55             isa => Bool
56             );
57              
58             has mode => (
59             is => 'ro',
60             isa => enum([qw(
61             change
62             tag
63             all
64             )]),
65             default => 'all',
66             );
67              
68             has deploy_variables => (
69             is => 'ro',
70             isa => HashRef,
71             lazy => 1,
72             default => sub { {} },
73             );
74              
75             has revert_variables => (
76             is => 'ro',
77             isa => HashRef,
78             lazy => 1,
79             default => sub { {} },
80             );
81              
82             sub _collect_deploy_vars {
83 33     33   3120 my ($self, $target) = @_;
84 33         750 my $cfg = $self->sqitch->config;
85             return (
86 33         151 %{ $cfg->get_section(section => 'core.variables') },
87 33         110 %{ $cfg->get_section(section => 'deploy.variables') },
88 33         647 %{ $target->variables }, # includes engine
89 33         272 %{ $self->deploy_variables }, # --set, --set-deploy
  33         1140  
90             );
91             }
92              
93             sub _collect_revert_vars {
94 35     35   11944 my ($self, $target) = @_;
95 35         781 my $cfg = $self->sqitch->config;
96             return (
97 35         151 %{ $cfg->get_section(section => 'core.variables') },
98 35         132 %{ $cfg->get_section(section => 'deploy.variables') },
99 35         119 %{ $cfg->get_section(section => 'revert.variables') },
100 35         738 %{ $target->variables }, # includes engine
101 35         284 %{ $self->revert_variables }, # --set, --set-revert
  35         1240  
102             );
103             }
104              
105             around options => sub {
106             my ($orig, $class) = @_;
107             return ($class->$orig), qw(
108             target|t=s
109             mode=s
110             verify!
111             set|s=s%
112             set-deploy|e=s%
113             set-revert|r=s%
114             log-only
115             lock-timeout=i
116             y
117             );
118             };
119              
120             around configure => sub {
121             my ( $orig, $class, $config, $opt ) = @_;
122             my $cmd = $class->command;
123              
124             # Command disabled in strict mode.
125             hurl {
126             ident => $cmd,
127             exitval => 2,
128             message => __x(
129             '"{command}" cannot be used in strict mode.\n'.
130             'Use explicity revert and deploy commands instead.',
131             command => $cmd,
132             ),
133             } if $config->get(
134             key => "$cmd.strict",
135             as => 'bool',
136             ) // $config->get(
137             key => 'revert.strict',
138             as => 'bool',
139             );
140              
141             my $params = $class->$orig($config, $opt);
142             for my $key (qw(log_only target lock_timeout)) {
143             $params->{$key} = $opt->{$key} if exists $opt->{$key};
144             }
145              
146             # Verify?
147             $params->{verify} = $opt->{verify}
148             // $config->get( key => "$cmd.verify", as => 'boolean' )
149             // $config->get( key => 'deploy.verify', as => 'boolean' )
150             // 0;
151             $params->{mode} = $opt->{mode}
152             || $config->get( key => "$cmd.mode" )
153             || $config->get( key => 'deploy.mode' )
154             || 'all';
155              
156             if ( my $vars = $opt->{set} ) {
157             # --set used for both revert and deploy.
158             $params->{revert_variables} = $params->{deploy_variables} = $vars;
159             }
160              
161             if ( my $vars = $opt->{set_deploy} ) {
162             # --set-deploy used only for deploy.
163             $params->{deploy_variables} = {
164             %{ $params->{deploy_variables} || {} },
165             %{ $vars },
166             };
167             }
168              
169             if ( my $vars = $opt->{set_revert} ) {
170             # --set-revert used only for revert.
171             $params->{revert_variables} = {
172             %{ $params->{revert_variables} || {} },
173             %{ $vars },
174             };
175             }
176              
177             $params->{no_prompt} = delete $opt->{y} // $config->get(
178             key => "$cmd.no_prompt",
179             as => 'bool',
180             ) // $config->get(
181             key => 'revert.no_prompt',
182             as => 'bool',
183             ) // 0;
184              
185             $params->{prompt_accept} = $config->get(
186             key => "$cmd.prompt_accept",
187             as => 'bool',
188             ) // $config->get(
189             key => 'revert.prompt_accept',
190             as => 'bool',
191             ) // 1;
192              
193             return $params;
194             };
195              
196             1;
197              
198             __END__
199              
200             =head1 Name
201              
202             App::Sqitch::Role::RevertDeployCommand - A command that reverts and deploys
203              
204             =head1 Synopsis
205              
206             package App::Sqitch::Command::rebase;
207             extends 'App::Sqitch::Command';
208             with 'App::Sqitch::Role::RevertDeployCommand';
209              
210             =head1 Description
211              
212             This role encapsulates the common attributes and methods required by commands
213             that both revert and deploy.
214              
215             =head1 Interface
216              
217             =head2 Class Methods
218              
219             =head3 C<options>
220              
221             my @opts = App::Sqitch::Command::checkout->options;
222              
223             Adds options common to the commands that revert and deploy.
224              
225             =head3 C<configure>
226              
227             Configures the options common to commands that revert and deploy.
228              
229             =head2 Attributes
230              
231             =head3 C<log_only>
232              
233             Boolean indicating whether to log the deploy without running the scripts.
234              
235             =head3 C<lock_timeout>
236              
237             The number of seconds to wait for an exclusive advisory lock on the target,
238             for engines that support the feature.
239              
240             =head3 C<no_prompt>
241              
242             Boolean indicating whether or not to prompt the user to really go through with
243             the revert.
244              
245             =head3 C<prompt_accept>
246              
247             Boolean value to indicate whether or not the default value for the prompt,
248             should the user hit C<return>, is to accept the prompt or deny it.
249              
250             =head3 C<strict>
251              
252             Boolean value to indicate whether or not strict mode is enabled; if
253             so, use of these commands is prohibited.
254              
255             =head3 C<target>
256              
257             The deployment target URI.
258              
259             =head3 C<verify>
260              
261             Boolean indicating whether or not to run verify scripts after deploying
262             changes.
263              
264             =head3 C<mode>
265              
266             Deploy mode, one of "change", "tag", or "all".
267              
268             =head1 See Also
269              
270             =over
271              
272             =item L<App::Sqitch::Command::rebase>
273              
274             The C<rebase> command reverts and deploys changes.
275              
276             =item L<App::Sqitch::Command::checkout>
277              
278             The C<checkout> command takes a VCS commit name, determines the last change in
279             common with the current commit, reverts to that change, then checks out the
280             named commit and re-deploys.
281              
282             =back
283              
284             =head1 Author
285              
286             David E. Wheeler <david@justatheory.com>
287              
288             =head1 License
289              
290             Copyright (c) 2012-2023 iovation Inc., David E. Wheeler
291              
292             Permission is hereby granted, free of charge, to any person obtaining a copy
293             of this software and associated documentation files (the "Software"), to deal
294             in the Software without restriction, including without limitation the rights
295             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
296             copies of the Software, and to permit persons to whom the Software is
297             furnished to do so, subject to the following conditions:
298              
299             The above copyright notice and this permission notice shall be included in all
300             copies or substantial portions of the Software.
301              
302             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
303             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
304             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
305             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
306             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
307             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308             SOFTWARE.
309              
310             =cut