File Coverage

blib/lib/CPAN/Upload/Tiny.pm
Criterion Covered Total %
statement 38 73 52.0
branch 7 28 25.0
condition 2 14 14.2
subroutine 9 14 64.2
pod 4 6 66.6
total 60 135 44.4


line stmt bran cond sub pod time code
1             package CPAN::Upload::Tiny;
2             $CPAN::Upload::Tiny::VERSION = '0.010';
3 1     1   71720 use strict;
  1         3  
  1         31  
4 1     1   6 use warnings;
  1         2  
  1         25  
5              
6 1     1   4 use Carp ();
  1         3  
  1         12  
7 1     1   6 use File::Basename ();
  1         2  
  1         12  
8 1     1   480 use MIME::Base64 ();
  1         1628  
  1         32  
9 1     1   739 use HTTP::Tiny;
  1         52804  
  1         42  
10 1     1   470 use HTTP::Tiny::Multipart;
  1         1188  
  1         34  
11 1     1   493 use Term::ReadKey 'ReadMode';
  1         2086  
  1         918  
12              
13             my $UPLOAD_URI = $ENV{CPAN_UPLOADER_UPLOAD_URI} || 'https://pause.perl.org/pause/authenquery?ACTION=add_uri';
14              
15             sub new {
16 0     0 1 0 my ($class, $user, $password) = @_;
17 0 0       0 Carp::croak('No user set') if not defined $user;
18 0 0       0 Carp::croak('No password set') if not defined $password;
19 0         0 return bless {
20             user => $user,
21             password => $password,
22             }, $class;
23             }
24              
25             sub new_from_config {
26 0     0 1 0 my ($class, $filename) = @_;
27 0         0 return $class->new(read_config_file($filename));
28             }
29              
30             sub prompt {
31 0     0 0 0 my ($mess, $mode) = @_;
32              
33 0         0 local $| = 1;
34 0         0 local $\;
35 0         0 print "$mess? ";
36              
37 0         0 ReadMode($mode);
38 0   0     0 my $ans = // '';
39 0         0 ReadMode(0);
40 0 0       0 print "\n" if $mode > 1;
41 0         0 chomp $ans;
42 0         0 return $ans;
43             }
44              
45             sub new_from_config_or_stdin {
46 0     0 1 0 my ($class, $filename) = @_;
47 0         0 my ($user, $pass) = read_config_file($filename);
48 0   0     0 $user ||= prompt("What is your PAUSE ID" , 'normal');
49 0   0     0 $pass ||= prompt("What is your PAUSE password", 'noecho');
50 0         0 return $class->new($user, $pass);
51             }
52              
53             sub upload_file {
54 0     0 1 0 my ($self, $filename) = @_;
55              
56 0 0       0 open my $fh, '<:raw', $filename or die "Could not open $filename: $!";
57 0         0 my $content = do { local $/; <$fh> };
  0         0  
  0         0  
58              
59 0         0 my $tiny = HTTP::Tiny->new(verify_SSL => 1);
60              
61 0         0 my $auth = 'Basic ' . MIME::Base64::encode("$self->{user}:$self->{password}", '');
62              
63             my $result = $tiny->post_multipart($UPLOAD_URI, {
64             HIDDENNAME => $self->{user},
65 0         0 CAN_MULTIPART => 1,
66             pause99_add_uri_httpupload => {
67             filename => File::Basename::basename($filename),
68             content => $content,
69             content_type => 'application/gzip',
70             },
71             pause99_add_uri_uri => '',
72             SUBMIT_pause99_add_uri_httpupload => ' Upload this file from my disk ',
73             }, { headers => { Authorization => $auth } });
74              
75 0 0       0 if (!$result->{success}) {
76 0 0       0 my $key = $result->{status} == 599 ? 'content' : 'reason';
77 0         0 die "Upload failed: $result->{$key}\n";
78             }
79              
80 0         0 return;
81             }
82              
83             sub read_config_file {
84 1   33 1 0 749 my $filename = shift || glob('~/.pause');
85 1 50       18 return unless -r $filename;
86              
87 1         3 my %conf;
88 1 50       2 if ( eval { require Config::Identity } ) {
  1         243  
89 0         0 %conf = Config::Identity->load($filename);
90 0 0       0 $conf{user} = delete $conf{username} unless $conf{user};
91             }
92             else { # Process .pause manually
93 1 50       40 open my $pauserc, '<', $filename or die "can't open $filename for reading: $!";
94              
95 1         18 while (<$pauserc>) {
96 2         7 chomp;
97 2 50       5 Carp::croak "$filename seems to be encrypted. Maybe you need to install Config::Identity?" if /BEGIN PGP MESSAGE/;
98              
99 2 50 33     13 next if not length or $_ =~ /^\s*#/;
100              
101 2 50       21 if (my ($k, $v) = / ^ \s* (user|password) \s+ (.+?) \s* $ /x) {
102 2 50       6 Carp::croak "Multiple entries for $k" if $conf{$k};
103 2         29 $conf{$k} = $v;
104             }
105             }
106             }
107              
108 1         10 return @conf{'user', 'password'};
109             }
110              
111             1;
112              
113             #ABSTRACT: A tiny CPAN uploader
114              
115             __END__