File Coverage

blib/lib/Ixchel/functions/file_get.pm
Criterion Covered Total %
statement 17 36 47.2
branch 0 12 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 24 56 42.8


line stmt bran cond sub pod time code
1             package Ixchel::functions::file_get;
2              
3 6     6   94628 use 5.006;
  6         47  
4 6     6   32 use strict;
  6         9  
  6         139  
5 6     6   39 use warnings;
  6         8  
  6         337  
6 6     6   449 use File::Slurp;
  6         40936  
  6         504  
7 6     6   35 use Exporter 'import';
  6         11  
  6         317  
8             our @EXPORT = qw(file_get);
9 6     6   4388 use LWP::UserAgent ();
  6         400378  
  6         1922  
10              
11             =head1 NAME
12              
13             Ixchel::functions::file_get - Fetches a file file via URL.
14              
15             =head1 VERSION
16              
17             Version 0.0.1
18              
19             =cut
20              
21             our $VERSION = '0.0.1';
22              
23             =head1 SYNOPSIS
24              
25             use Ixchel::functions::file_get;
26              
27             my $file=file_get(url=>'https://raw.githubusercontent.com/quadrantsec/sagan/main/etc/sagan.yaml');
28              
29             =head1 Functions
30              
31             =head2 file_get
32              
33             Any protocol understood via L may be used.
34              
35             If the $ENV variables below are set, they will be used for proxy info.
36              
37             $ENV{FTP_PROXY}
38             $ENV{HTTP_PROXY}
39             $ENV{HTTPS_PROXY}
40              
41             =cut
42              
43             sub file_get {
44 0     0 1   my (%opts) = @_;
45              
46 0 0         if ( !defined( $opts{url} ) ) {
47 0           die('url not specified');
48             }
49              
50 0           my $content;
51 0           eval {
52 0           my $ua = LWP::UserAgent->new( timeout => 10 );
53 0 0         if ( defined( $ENV{HTTP_PROXY} ) ) {
54 0           $ua->proxy( ['http'], $ENV{HTTP_PROXY} );
55             }
56 0 0         if ( defined( $ENV{HTTPS_PROXY} ) ) {
57 0           $ua->proxy( ['https'], $ENV{HTTPS_PROXY} );
58             }
59 0 0         if ( defined( $ENV{FTP_PROXY} ) ) {
60 0           $ua->proxy( ['ftp'], $ENV{FTP_PROXY} );
61             }
62              
63 0           my $response = $ua->get( $opts{url} );
64              
65 0 0         if ( $response->is_success ) {
66 0           $content = $response->decoded_content;
67             } else {
68 0           die( $response->status_line );
69             }
70             };
71 0 0         if ($@) {
72 0           die( 'Fetching "' . $opts{url} . '" failed' ... $@ );
73             }
74              
75 0           return $content;
76             } ## end sub file_get
77              
78             1;