File Coverage

blib/lib/CPAN/Upload/Tiny.pm
Criterion Covered Total %
statement 35 70 50.0
branch 7 28 25.0
condition 2 17 11.7
subroutine 8 13 61.5
pod 3 6 50.0
total 55 134 41.0


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