File Coverage

blib/lib/App/PM/Website/Command/Install.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1 2     2   1786 use strict;
  2         4  
  2         66  
2 2     2   11 use warnings;
  2         4  
  2         96  
3              
4             package App::PM::Website::Command::Install;
5             {
6             $App::PM::Website::Command::Install::VERSION = '0.131611';
7             }
8 2     2   11 use base 'App::PM::Website::Command';
  2         3  
  2         161  
9 2     2   1791 use Net::Netrc;
  2         31105  
  2         71  
10 2     2   919 use HTTP::DAV;
  0            
  0            
11             use Data::Dumper;
12              
13             #ABSTRACT: install the built website into production via caldav
14              
15             sub options
16             {
17             my ($class, $app) = @_;
18             return (
19             [ 'url=s' => 'path to webdav directory' ],
20             [ 'build-dir=s' => 'path to local rendered files' ,
21             { default => 'website' }],
22             [ 'filename=s' => 'upload name, rather than index.html' ,
23             {default => 'index.html'}],
24             [ 'username=s' => 'username for webdav, override .netrc' ],
25             [ 'password=s' => 'password for webdav, override .netrc' ],
26             [ 'certificate=s' => 'path to ca certificate' ],
27             );
28             }
29              
30             sub validate
31             {
32             my ($self, $opt, $args ) = @_;
33              
34             $self->validate_certificate($opt);
35             $self->validate_url($opt);
36             $self->validate_login($opt);
37              
38             if(@$args)
39             {
40             die $self->usage_error("no arguments allowed")
41             }
42             }
43             sub validate_certificate
44             {
45             my ($self, $opt) = @_;
46             my $c = $self->{config}{config}{website};
47             $opt->{certificate} ||= $c->{certificate};
48              
49             if ($opt->{certificate} && ! -f $opt->{certificate} )
50             {
51             die $self->usage_error("could not find certificate file: $opt->{certificate}");
52              
53             }
54              
55             return 1; #certificate is optional.
56             }
57             sub validate_url
58             {
59             my ($self, $opt ) = @_;
60             my $c = $self->{config}{config}{website};
61              
62             $opt->{url} ||= $c->{url};
63             die $self->usage_error( "url must be defined on command line or in config file")
64             unless $opt->{url};
65             }
66             sub validate_login
67             {
68             my ( $self, $opt ) = @_;
69              
70             my $c = $self->{config}{config}{website};
71             my $url = $opt->{url};
72             my $machine = $opt->{machine} || $c->{machine};
73              
74             $opt->{username} ||= $c->{username};
75             $opt->{password} ||= $c->{password};
76              
77             return 1 if ( $opt->{username} && $opt->{password} );
78              
79             if( $machine )
80             {
81             my $mach = Net::Netrc->lookup($machine);
82             if ( defined $mach )
83             {
84             $opt->{username} ||= $mach->login();
85             $opt->{password} ||= $mach->password();
86             }
87             else
88             {
89             warn "machine '$machine' not found in .netrc"
90             }
91             }
92              
93             return 1 if ( $opt->{username} && $opt->{password} );
94              
95             die $self->usage_error(
96             "username and password must be defined on the command line, config file or in .netrc"
97             );
98             }
99              
100             sub execute
101             {
102             my ( $self, $opt, $args ) = @_;
103              
104             my $webdav = HTTP::DAV->new();
105             if( $opt->{certificate} )
106             {
107             print Dumper { certificate => $opt->{certificate} };
108             my $ua = $webdav->get_user_agent;
109             if ( $ua->can('ssl_opts') )
110             {
111             $ua->ssl_opts(SSL_ca_file => $opt->{certificate});
112             }
113             else
114             {
115             warn "Old version of LWP::UserAgent doesn't support ssl_opts"
116             }
117             }
118             my %webdav_credentials = (
119             -user => $opt->{username},
120             -pass => $opt->{password},
121             -url => $opt->{url},
122             -realm => "groups.perl.org",
123             );
124             print Dumper { credentials => \%webdav_credentials };
125             $webdav->credentials(%webdav_credentials);
126             $webdav->open( -url => $opt->{url} )
127             or die sprintf( "failed to open url [%s] : %s\n",
128             $opt->{url}, $webdav->message() );
129              
130             my %put_options = (
131             -local => "$opt->{build_dir}/$opt->{filename}",
132             -url => $opt->{url},
133             );
134             print Dumper { put_options => \%put_options };
135             my $success = $opt->{dry_run} ? 1 : $webdav->put(%put_options);
136              
137             die sprintf(
138             "failed to put file %s/%s to url %s : %s\n",
139             $opt->{build_dir}, $opt->{filename},
140             $opt->{url}, $webdav->message(),
141             ) unless $success;
142              
143             return $success;
144             }
145             1;
146              
147             __END__