File Coverage

blib/lib/Footprintless/App/Command/service.pm
Criterion Covered Total %
statement 37 43 86.0
branch 6 14 42.8
condition 0 6 0.0
subroutine 8 8 100.0
pod 3 3 100.0
total 54 74 72.9


line stmt bran cond sub pod time code
1 5     5   2582 use strict;
  5         8  
  5         139  
2 5     5   20 use warnings;
  5         49  
  5         213  
3              
4             package Footprintless::App::Command::service;
5             $Footprintless::App::Command::service::VERSION = '1.28';
6 5     5   23 use Footprintless::App -command;
  5         9  
  5         23  
7 5     5   1491 use Footprintless::Util qw(exit_due_to);
  5         9  
  5         246  
8 5     5   24 use Log::Any;
  5         8  
  5         20  
9              
10             my $logger = Log::Any->get_logger();
11              
12             # ABSTRACT: Performs an action on one or more services.
13             # PODNAME: Footprintless::App::Command::service
14              
15             sub execute {
16 3     3 1 12 my ( $self, $opts, $args ) = @_;
17 3         7 my ( $coordinate, $action ) = @$args;
18              
19 3         4 foreach my $target ( @{ $self->{targets} } ) {
  3         7  
20 4         10 $logger->debugf( 'executing %s for %s', $action, $target->{coordinate} );
21 4         33 eval { $target->execute($action); };
  4         8  
22 4 50       48 if ($@) {
23 0 0 0     0 if ( ref($@) && $@->isa('Footprintless::InvalidEntityException') ) {
24 0         0 $self->usage_error($@);
25             }
26 0         0 exit_due_to( $@, 1 );
27             }
28             }
29             }
30              
31             sub usage_desc {
32 3     3 1 132 return "fpl service SERVICE_COORD ACTION";
33             }
34              
35             sub validate_args {
36 3     3 1 921 my ( $self, $opt, $args ) = @_;
37 3         7 my ( $coordinate, $action, @sub_coordinates ) = @$args;
38              
39 3 50       6 $self->usage_error("coordinate is required") unless ($coordinate);
40 3 50       6 $self->usage_error("action is required") unless ($action);
41              
42 3         10 my $footprintless = $self->app()->footprintless();
43              
44             my @target_coordinates =
45             @sub_coordinates
46 3 100       9 ? map {"$coordinate.$_"} @sub_coordinates
  2         6  
47             : ($coordinate);
48              
49 3         5 my @targets = ();
50 3         6 foreach my $target_coordinate (@target_coordinates) {
51 4         5 eval { push( @targets, $footprintless->service($target_coordinate) ); };
  4         11  
52 4 50       10 if ($@) {
53 0 0 0     0 if ( ref($@) && $@->isa('Footprintless::InvalidEntityException') ) {
54 0         0 $self->usage_error($@);
55             }
56             else {
57 0         0 $self->usage_error("invalid coordinate [$target_coordinate]: $@");
58             }
59             }
60             }
61 3         11 $self->{targets} = \@targets;
62             }
63              
64             1;
65              
66             __END__