File Coverage

blib/lib/OTRS/OPM/Installer/Utils/File.pm
Criterion Covered Total %
statement 82 100 82.0
branch 19 30 63.3
condition 0 2 0.0
subroutine 19 22 86.3
pod 0 2 0.0
total 120 156 76.9


line stmt bran cond sub pod time code
1             package OTRS::OPM::Installer::Utils::File;
2             $OTRS::OPM::Installer::Utils::File::VERSION = '0.05';
3             # ABSTRACT: File related utility functions
4              
5 11     11   742143 use v5.10;
  11         110  
6              
7 11     11   73 use strict;
  11         28  
  11         263  
8 11     11   67 use warnings;
  11         47  
  11         376  
9              
10 11     11   6277 use File::HomeDir;
  11         64240  
  11         571  
11 11     11   84 use File::Spec;
  11         23  
  11         220  
12 11     11   6983 use File::Temp;
  11         197359  
  11         825  
13 11     11   7618 use HTTP::Tiny;
  11         363112  
  11         502  
14 11     11   5130 use HTTP::Tiny::FileProtocol;
  11         287851  
  11         434  
15 11     11   5260 use IO::All;
  11         99499  
  11         91  
16 11     11   5166 use Moo;
  11         83405  
  11         68  
17 11     11   17677 use OTRS::OPM::Installer::Logger;
  11         37  
  11         416  
18 11     11   5791 use OTRS::OPM::Installer::Utils::Config;
  11         41  
  11         402  
19 11     11   3404 use OTRS::OPM::Installer::Types;
  11         32  
  11         318  
20 11     11   5010 use OTRS::Repository;
  11         897276  
  11         463  
21 11     11   101 use Regexp::Common qw(URI);
  11         25  
  11         72  
22 11     11   6715 use Types::Standard qw(ArrayRef Str Bool);
  11         590289  
  11         148  
23              
24             our $ALLOWED_SCHEME = [ 'HTTP', 'file' ];
25              
26             has repositories => ( is => 'ro', isa => ArrayRef[Str], default => \&_repository_list );
27             has package => ( is => 'ro', isa => Str, required => 1 );
28             has otrs_version => ( is => 'ro', isa => Str, required => 1 );
29             has version => ( is => 'ro', isa => Str );
30             has verbose => ( is => 'ro', isa => Bool );
31             has logger => ( is => 'ro', default => sub{ OTRS::OPM::Installer::Logger->new } );
32             has rc_config => ( is => 'ro', lazy => 1, default => \&_rc_config );
33             has conf => ( is => 'ro' );
34              
35             sub list_available {
36 0     0 0 0 my $self = shift;
37              
38 0 0       0 my @repositories = @{ $self->repositories || [] };
  0         0  
39              
40 0         0 for my $repo_url ( @repositories ) {
41 0 0       0 $repo_url .= '/otrs.xml' if '/otrs.xml' ne substr $repo_url, -9;
42             }
43              
44 0         0 my $repo = OTRS::Repository->new(
45             sources => \@repositories,
46             );
47              
48 0         0 my $otrs_version = $self->otrs_version;
49 0         0 $otrs_version =~ s{\.\d+$}{};
50              
51 0         0 return $repo->list(
52             otrs => $otrs_version,
53             details => 1,
54             );
55             }
56              
57             sub resolve_path {
58 12     12 0 66568 my ($self) = @_;
59              
60 12         28 my $path;
61              
62 12         69 my $package = $self->package;
63 12 100       50 if ( $self->_is_url( $package ) ) {
    100          
64             # download file
65 2         10 $path = $self->_download( $package );
66             }
67             elsif ( -f $package ) {
68             # do nothing, file already exists
69 1         5 $path = $package;
70             }
71             else {
72 9 50       39 my @repositories = @{ $self->repositories || [] };
  9         86  
73              
74 9         33 for my $repo ( @repositories ) {
75 9 100       63 $repo .= '/otrs.xml' if '/otrs.xml' ne substr $repo, -9;
76             }
77              
78 9 50       46 say "Searching these repositories: @repositories" if $self->verbose;
79              
80 9         148 my $repo = OTRS::Repository->new(
81             sources => \@repositories,
82             );
83              
84 9         34632 my ($otrs) = $self->otrs_version =~ m{\A(\d+\.\d+)};
85              
86 9         74 my ($url) = $repo->find(
87             name => $package,
88             otrs => $otrs,
89             version => $self->version,
90             );
91              
92 9 50 0     24009 say "Found ", $url // '' if $self->verbose;
93              
94 9 100       90 return if !$url;
95              
96 6         24 $path = $self->_download( $url );
97             }
98              
99 9         272 return $path;
100             }
101              
102             sub _repository_list {
103 0     0   0 my ($self) = @_;
104              
105 0         0 my $config = $self->rc_config;
106 0         0 my $repositories = $config->{repository};
107              
108 0 0       0 return [] if !$repositories;
109 0         0 return $repositories;
110             }
111              
112             sub _is_url {
113 19     19   2966 my ($self, $package) = @_;
114              
115 19 50       78 my @allowed_schemes = ref $ALLOWED_SCHEME ? @{ $ALLOWED_SCHEME } : $ALLOWED_SCHEME;
  19         62  
116              
117 19         34 my $matches;
118              
119             SCHEME:
120 19         48 for my $scheme ( @allowed_schemes ) {
121             my $regex = ( lc $scheme eq 'http' ) ?
122             $RE{URI}{HTTP}{-scheme => qr/https?/} :
123 34 100       5003 $RE{URI}{$scheme};
124              
125 34 100       2095 if ( $package =~ m{\A$regex\z} ) {
126 7         1076 $matches++;
127 7         33 last SCHEME;
128             }
129             }
130              
131 19 100       2441 return if !$matches;
132 7         27 return 1;
133             }
134              
135             sub _download {
136 8     8   24 my ($self, $url) = @_;
137              
138 8         78 my $file = File::Temp->new->filename;
139 8         4135 my $response = HTTP::Tiny->new->mirror( $url, $file );
140              
141 8         16401 $self->logger->notice( area => 'download', file => $file, success => $response->{success} );
142              
143 8 50       5217 return if !$response->{success};
144 8         1286 return $file;
145             }
146              
147             sub _rc_config {
148 0     0     my ($self) = @_;
149              
150 0           my $utils = OTRS::OPM::Installer::Utils::Config->new( conf => $self->conf );
151 0           my $config = $utils->rc_config;
152              
153 0           return $config;
154             }
155              
156             1;
157              
158             __END__