File Coverage

blib/lib/Shell/Amazon/S3/ConfigLoader.pm
Criterion Covered Total %
statement 15 35 42.8
branch 0 2 0.0
condition 0 10 0.0
subroutine 5 9 55.5
pod 0 4 0.0
total 20 60 33.3


line stmt bran cond sub pod time code
1             package Shell::Amazon::S3::ConfigLoader;
2 1     1   4 use Moose;
  1         1  
  1         6  
3 1     1   4737 use YAML;
  1         5165  
  1         47  
4 1     1   462 use File::HomeDir;
  1         4016  
  1         57  
5 1     1   376 use Path::Class qw(file);
  1         28501  
  1         56  
6 1     1   640 use ExtUtils::MakeMaker ();
  1         80833  
  1         290  
7              
8             has 'changed' => (
9             is => 'rw',
10             isa => 'Int',
11             default => sub {0},
12             );
13              
14             has 'conf' => (
15             is => 'rw',
16             default => sub { file( File::HomeDir->my_home, ".psh3ll" ) },
17             );
18              
19             sub prompt {
20 0     0 0   my ( $self, $prompt ) = @_;
21 0           my $value = ExtUtils::MakeMaker::prompt($prompt);
22 0           $self->changed( $self->changed + 1 );
23 0           return $value;
24             }
25              
26             # TODO: More testable
27             # we need to inject access key and secret access key outsied
28             sub load {
29 0     0 0   my $self = shift;
30 0   0       my $config = eval { YAML::LoadFile( $self->conf ) } || {};
31 0   0       $config->{aws_access_key_id} ||= $self->prompt("AWS access key:");
32             $config->{aws_secret_access_key}
33 0   0       ||= $self->prompt("AWS secret access key:");
34 0           $self->save($config);
35 0           return $config;
36             }
37              
38             sub update {
39 0     0 0   my ( $self, $key, $value ) = @_;
40 0   0       my $config = eval { YAML::LoadFile( $self->conf ) } || {};
41 0           $config->{$key} = $value;
42 0           $self->changed( $self->changed + 1 );
43 0           $self->save($config);
44             }
45              
46             sub save {
47 0     0 0   my ( $self, $conf ) = @_;
48 0 0         if ( $self->changed ) {
49 0           YAML::DumpFile( $self->conf, $conf );
50 0           chmod 0600, $self->conf;
51             }
52 0           $self->changed(0);
53             }
54              
55             __PACKAGE__->meta->make_immutable;
56              
57             1;