| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Sky::Config::Validate; | 
| 2 |  |  |  |  |  |  | $App::Sky::Config::Validate::VERSION = '0.4.0'; | 
| 3 | 1 |  |  | 1 |  | 85626 | use strict; | 
|  | 1 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 5 | use Carp (); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 13 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 509 | use Moo; | 
|  | 1 |  |  |  |  | 10783 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 10 | 1 |  |  | 1 |  | 1807 | use MooX 'late'; | 
|  | 1 |  |  |  |  | 10063 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 1 |  |  | 1 |  | 23908 | use Scalar::Util qw(reftype); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 48 |  | 
| 13 | 1 |  |  | 1 |  | 546 | use List::MoreUtils qw(notall); | 
|  | 1 |  |  |  |  | 12000 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | has 'config' => ( isa => 'HashRef', is => 'ro', required => 1, ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub _sorted_keys | 
| 19 |  |  |  |  |  |  | { | 
| 20 | 2 |  |  | 2 |  | 5 | my $hash_ref = shift; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 2 |  |  |  |  | 11 | return sort { $a cmp $b } keys(%$hash_ref); | 
|  | 3 |  |  |  |  | 7 |  | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub _validate_section | 
| 26 |  |  |  |  |  |  | { | 
| 27 | 3 |  |  | 3 |  | 7 | my ( $self, $site_name, $sect_name, $sect_conf ) = @_; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 3 |  |  |  |  | 4 | foreach my $string_key (qw( basename_re target_dir)) | 
| 30 |  |  |  |  |  |  | { | 
| 31 | 6 |  |  |  |  | 10 | my $v = $sect_conf->{$string_key}; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 6 | 50 | 33 |  |  | 33 | if ( | 
|  |  |  | 33 |  |  |  |  | 
| 34 |  |  |  |  |  |  | not(   defined($v) | 
| 35 |  |  |  |  |  |  | && ref($v) eq '' | 
| 36 |  |  |  |  |  |  | && $v =~ /\S/ ) | 
| 37 |  |  |  |  |  |  | ) | 
| 38 |  |  |  |  |  |  | { | 
| 39 | 0 |  |  |  |  | 0 | die | 
| 40 |  |  |  |  |  |  | "Section '$sect_name' at site '$site_name' must contain a non-empty $string_key"; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 3 |  |  |  |  | 7 | return; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub _validate_site | 
| 48 |  |  |  |  |  |  | { | 
| 49 | 1 |  |  | 1 |  | 4 | my ( $self, $site_name, $site_conf ) = @_; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 1 |  |  |  |  | 2 | my $base_upload_cmd = $site_conf->{base_upload_cmd}; | 
| 52 | 1 | 50 |  |  |  | 4 | if ( ref($base_upload_cmd) ne 'ARRAY' ) | 
| 53 |  |  |  |  |  |  | { | 
| 54 | 0 |  |  |  |  | 0 | die "base_upload_cmd for site '$site_name' is not an array."; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 1 | 50 |  | 5 |  | 13 | if ( notall { defined($_) && ref($_) eq '' } @$base_upload_cmd ) | 
|  | 5 | 50 |  |  |  | 19 |  | 
| 58 |  |  |  |  |  |  | { | 
| 59 | 0 |  |  |  |  | 0 | die "base_upload_cmd for site '$site_name' must contain only strings."; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 1 |  |  |  |  | 4 | foreach my $kk (qw(dest_upload_prefix dest_upload_url_prefix)) | 
| 63 |  |  |  |  |  |  | { | 
| 64 | 2 |  |  |  |  | 5 | my $s = $site_conf->{$kk}; | 
| 65 | 2 | 50 | 33 |  |  | 18 | if ( not( defined($s) && ( ref($s) eq '' ) && ( $s =~ m/\S/ ) ) ) | 
|  |  |  | 33 |  |  |  |  | 
| 66 |  |  |  |  |  |  | { | 
| 67 | 0 |  |  |  |  | 0 | die "$kk for site '$site_name' is not a string."; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 1 |  |  |  |  | 4 | my $sections = $site_conf->{sections}; | 
| 72 | 1 | 50 |  |  |  | 4 | if ( ref($sections) ne 'HASH' ) | 
| 73 |  |  |  |  |  |  | { | 
| 74 | 0 |  |  |  |  | 0 | die "Sections for site '$site_name' is not a hash."; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 1 |  |  |  |  | 2 | foreach my $sect_name ( _sorted_keys($sections) ) | 
| 78 |  |  |  |  |  |  | { | 
| 79 |  |  |  |  |  |  | $self->_validate_section( $site_name, $sect_name, | 
| 80 | 3 |  |  |  |  | 8 | $sections->{$sect_name} ); | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 1 |  |  |  |  | 3 | return; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub is_valid | 
| 87 |  |  |  |  |  |  | { | 
| 88 | 1 |  |  | 1 | 1 | 2459 | my ($self) = @_; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 1 |  |  |  |  | 7 | my $config = $self->config(); | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # Validate the configuration | 
| 93 |  |  |  |  |  |  | { | 
| 94 | 1 | 50 |  |  |  | 3 | if ( !exists( $config->{default_site} ) ) | 
|  | 1 |  |  |  |  | 4 |  | 
| 95 |  |  |  |  |  |  | { | 
| 96 | 0 |  |  |  |  | 0 | die "A 'default_site' key must be present in the configuration."; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 1 |  |  |  |  | 3 | my $sites = $config->{sites}; | 
| 100 | 1 | 50 |  |  |  | 5 | if ( ref($sites) ne 'HASH' ) | 
| 101 |  |  |  |  |  |  | { | 
| 102 | 0 |  |  |  |  | 0 | die "sites key must be a hash."; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 1 |  |  |  |  | 4 | foreach my $name ( _sorted_keys($sites) ) | 
| 106 |  |  |  |  |  |  | { | 
| 107 | 1 |  |  |  |  | 12 | $self->_validate_site( $name, $sites->{$name} ); | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 1 |  |  |  |  | 3 | return; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | 1; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | __END__ |