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   3000 use strict;
  5         9  
  5         132  
2 5     5   22 use warnings;
  5         51  
  5         220  
3              
4             package Footprintless::App::Command::service;
5             $Footprintless::App::Command::service::VERSION = '1.29';
6 5     5   24 use Footprintless::App -command;
  5         8  
  5         24  
7 5     5   1457 use Footprintless::Util qw(exit_due_to);
  5         12  
  5         253  
8 5     5   23 use Log::Any;
  5         10  
  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 16 my ( $self, $opts, $args ) = @_;
17 3         7 my ( $coordinate, $action ) = @$args;
18              
19 3         4 foreach my $target ( @{ $self->{targets} } ) {
  3         7  
20 4         13 $logger->debugf( 'executing %s for %s', $action, $target->{coordinate} );
21 4         34 eval { $target->execute($action); };
  4         8  
22 4 50       87 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 137 return "fpl service SERVICE_COORD ACTION";
33             }
34              
35             sub validate_args {
36 3     3 1 995 my ( $self, $opt, $args ) = @_;
37 3         7 my ( $coordinate, $action, @sub_coordinates ) = @$args;
38              
39 3 50       7 $self->usage_error("coordinate is required") unless ($coordinate);
40 3 50       6 $self->usage_error("action is required") unless ($action);
41              
42 3         11 my $footprintless = $self->app()->footprintless();
43              
44             my @target_coordinates =
45             @sub_coordinates
46 3 100       17 ? map {"$coordinate.$_"} @sub_coordinates
  2         6  
47             : ($coordinate);
48              
49 3         6 my @targets = ();
50 3         6 foreach my $target_coordinate (@target_coordinates) {
51 4         5 eval { push( @targets, $footprintless->service($target_coordinate) ); };
  4         13  
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         13 $self->{targets} = \@targets;
62             }
63              
64             1;
65              
66             __END__