blib/lib/AWS/CLI/Config.pm

filter: Statementstmt 90.5% 96 / 106 Branchbran 70.0% 21 / 30 Conditioncond 45.1% 23 / 51 MC/DCmcdc 15.1% 5 / 33 Subroutinesub 86.9% 20 / 23 Podpod 100% 2 / 2 totaltotal 68.1% 167 / 245 SCAR 39.6
CC
34
Cov
75%
credentials
30.0
__ANON__
10.4
_parse
10.2
CRAP 52.4
↑ Summary Build »
1 package AWS::CLI::Config;
2 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = "0.03";
11  
12 5 my $DEFAULT_PROFILE = 'default';
13  
14 5 my $CREDENTIALS;
15 my %CREDENTIALS_PROFILE_OF;
16 5 my $CONFIG;
17 5 my %CONFIG_PROFILE_OF;
18  
19 BEGIN: {
20 5 my %accessor_of = (
21 access_key_id => +{ env => 'AWS_ACCESS_KEY_ID', key => 'aws_access_key_id' },
22 secret_access_key => +{ env => 'AWS_SECRET_ACCESS_KEY', key => 'aws_secret_access_key' },
23 session_token => +{ env => 'AWS_SESSION_TOKEN', key => 'aws_session_token' },
24 region => +{ env => 'AWS_DEFAULT_REGION' },
25 output => +{},
26 );
27  
28 MK_ACCESSOR: {
29 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 for my $attr (keys %accessor_of) {
31 25 my $func = __PACKAGE__ . "::$attr";
32 25 *{$func} = _mk_accessor($attr, %{$accessor_of{$attr}});
33 }
34 }
35 }
36  
37 sub _mk_accessor {
38 25 ▶ my $attr = shift;
Subroutine _mk_accessor: called CC=2 SCAR=7.2
39 25 my %opt = @_;
40  
41 25 my $env_var = $opt{env};
42 25 ▶ my $profile_key = $opt{key} || $attr;
Condition66%2/3
$opt{'key'} || $attr
l!l&&r!l&&!r
15100
MC/DC0%0/2
$opt{'key'} || $attr
$opt{'key'} $attr
Truth table66%2/3
A || B
A= $opt{'key'}B= $attr
ABresult
1X1
011
000
43  
44 return sub {
45 5 ▶ if ($env_var && exists $ENV{$env_var} && $ENV{$env_var}) {
Subroutine __ANON__: called CC=9 SCAR=23.4
Branch: 1 branch
BranchTrueFalseTotal
if ($env_var and exists $ENV{$env_var} and $ENV{$env_var}) 1 4 5
Condition83%5/6
$env_var and exists $ENV{$env_var} and $ENV{$env_var}
!ll&&!rl&&r
221
$env_var and exists $ENV{$env_var}
!ll&&!rl&&r
203
MC/DC66%2/3
$env_var and exists $ENV{$env_var} and $ENV{$env_var}
$env_var exists $ENV{$env_var} $ENV{$env_var}
Truth table75%3/4
A and B and C
A= $env_varB= exists $ENV{$env_var}C= $ENV{$env_var}
ABCresult
0XX0
10X0
1100
1111
46 1 return $ENV{$env_var};
47 }
48  
49 4 ▶ my $profile = shift || _default_profile();
Condition33%1/3
shift() || _default_profile()
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() || _default_profile()
shift() _default_profile()
Truth table33%1/3
A || B
A= shift()B= _default_profile()
ABresult
1X1
011
000
50  
51 4 my $credentials = credentials($profile);
52 4 ▶ if ($credentials && $credentials->$profile_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($credentials and $credentials->$profile_key) 2 2 4
Condition66%2/3
$credentials and $credentials->$profile_key
!ll&&!rl&&r
202
MC/DC50%1/2
$credentials and $credentials->$profile_key
$credentials $credentials->$profile_key
Truth table66%2/3
A and B
A= $credentialsB= $credentials->$profile_key
ABresult
0X0
100
111
53 2 return $credentials->$profile_key;
54 }
55  
56 2 my $config = config($profile);
57 2 ▶ if ($config && $config->$profile_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config and $config->$profile_key) 2 0 2
Condition33%1/3
$config and $config->$profile_key
!ll&&!rl&&r
002
MC/DC0%0/2
$config and $config->$profile_key
$config $config->$profile_key
Truth table33%1/3
A and B
A= $configB= $config->$profile_key
ABresult
0X0
100
111
58 2 return $config->$profile_key;
59 }
60  
61 0 return undef;
62 25 };
63 }
64  
65 sub credentials {
66 0 ▶ my $profile = shift || _default_profile();
Subroutine credentials: not called CC=5 SCAR=34.0
Pod documented
Condition0%0/3
shift() || _default_profile()
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || _default_profile()
shift() _default_profile()
Truth table0%0/3
A || B
A= shift()B= _default_profile()
ABresult
1X1
011
000
67 $CREDENTIALS ||= sub {
68 0 ▶ my $path = File::Spec->catfile(_default_dir(), 'credentials');
Subroutine __ANON__: not called CC=2 SCAR=17.9
69 0 ▶ return +{} unless (-r $path);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 0 0 0
70 0 return _parse($path);
71 0 ▶ }->();
Condition0%0/3
$CREDENTIALS ||= sub { my $path = "File::Spec"->catfile(_default_dir(), "credentials"); return {} unless -r $path; return _parse($path); } ->()
l!l&&r!l&&!r
000
MC/DC0%0/2
$CREDENTIALS ||= sub { my $path = "File::Spec"->catfile(_default_dir(), "credentials"); return {} unless -r $path; return _parse($path); } ->()
$CREDENTIALS sub { my $path = "File::Spec"->catfile(_default_dir(), "credentials"); return {} unless -r $path; return _parse($path); } ->()
Truth table0%0/3
A ||= B
A= $CREDENTIALSB= sub { my $path = "File::Spec"->catfile(_default_dir(), "credentials"); return {} unless -r $path; return _parse($path); } ->()
ABresult
1X1
011
000
72 0 ▶ return unless (exists $CREDENTIALS->{$profile});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $CREDENTIALS->{$profile} 0 0 0
73 0 ▶ $CREDENTIALS_PROFILE_OF{$profile} ||= AWS::CLI::Config::Profile->_new($CREDENTIALS->{$profile});
Condition0%0/3
$CREDENTIALS_PROFILE_OF{$profile} ||= "AWS::CLI::Config::Profile"->_new($CREDENTIALS->{$profile})
l!l&&r!l&&!r
000
MC/DC0%0/2
$CREDENTIALS_PROFILE_OF{$profile} ||= "AWS::CLI::Config::Profile"->_new($CREDENTIALS->{$profile})
$CREDENTIALS_PROFILE_OF{$profile} "AWS::CLI::Config::Profile"->_new($CREDENTIALS->{$profile})
Truth table0%0/3
A ||= B
A= $CREDENTIALS_PROFILE_OF{$profile}B= "AWS::CLI::Config::Profile"->_new($CREDENTIALS->{$profile})
ABresult
1X1
011
000
74 0 return $CREDENTIALS_PROFILE_OF{$profile};
75 }
76  
77 sub config {
78 3 ▶ my $profile = shift || _default_profile();
Subroutine config: called CC=5 SCAR=17.5
Pod documented
Condition66%2/3
shift() || _default_profile()
l!l&&r!l&&!r
210
MC/DC0%0/2
shift() || _default_profile()
shift() _default_profile()
Truth table66%2/3
A || B
A= shift()B= _default_profile()
ABresult
1X1
011
000
79  
80 $CONFIG ||= sub {
81 my $path
82 = (exists $ENV{AWS_CONFIG_FILE} && $ENV{AWS_CONFIG_FILE})
83 ? $ENV{AWS_CONFIG_FILE}
84 1 ▶ : File::Spec->catfile(_default_dir(), 'config');
Subroutine __ANON__: called CC=4 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'AWS_CONFIG_FILE'} && $ENV{'AWS_CONFIG_FILE'} ? : 1 0 1
Condition33%1/3
exists $ENV{"AWS_CONFIG_FILE"} and $ENV{"AWS_CONFIG_FILE"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $ENV{"AWS_CONFIG_FILE"} and $ENV{"AWS_CONFIG_FILE"}
exists $ENV{"AWS_CONFIG_FILE"} $ENV{"AWS_CONFIG_FILE"}
Truth table33%1/3
A and B
A= exists $ENV{"AWS_CONFIG_FILE"}B= $ENV{"AWS_CONFIG_FILE"}
ABresult
0X0
100
111
85 1 ▶ return +{} unless (-r $path);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 0 1 1
86  
87 1 return _parse($path);
88 3 ▶ }->();
Condition66%2/3
$CONFIG ||= sub { my $path = exists $ENV{"AWS_CONFIG_FILE"} && $ENV{"AWS_CONFIG_FILE"} ? $ENV{"AWS_CONFIG_FILE"} : "File::Spec"->catfile(_default_dir(), "config"); return {} unless -r $path; return _parse($path); } ->()
l!l&&r!l&&!r
210
MC/DC0%0/2
$CONFIG ||= sub { my $path = exists $ENV{"AWS_CONFIG_FILE"} && $ENV{"AWS_CONFIG_FILE"} ? $ENV{"AWS_CONFIG_FILE"} : "File::Spec"->catfile(_default_dir(), "config"); return {} unless -r $path; return _parse($path); } ->()
$CONFIG sub { my $path = exists $ENV{"AWS_CONFIG_FILE"} && $ENV{"AWS_CONFIG_FILE"} ? $ENV{"AWS_CONFIG_FILE"} : "File::Spec"->catfile(_default_dir(), "config"); return {} unless -r $path; return _parse($path); } ->()
Truth table66%2/3
A ||= B
A= $CONFIGB= sub { my $path = exists $ENV{"AWS_CONFIG_FILE"} && $ENV{"AWS_CONFIG_FILE"} ? $ENV{"AWS_CONFIG_FILE"} : "File::Spec"->catfile(_default_dir(), "config"); return {} unless -r $path; return _parse($path); } ->()
ABresult
1X1
011
000
89  
90 3 ▶ return unless (exists $CONFIG->{$profile});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $CONFIG->{$profile} 1 2 3
91 2 ▶ $CONFIG_PROFILE_OF{$profile} ||= AWS::CLI::Config::Profile->_new($CONFIG->{$profile});
Condition33%1/3
$CONFIG_PROFILE_OF{$profile} ||= "AWS::CLI::Config::Profile"->_new($CONFIG->{$profile})
l!l&&r!l&&!r
020
MC/DC0%0/2
$CONFIG_PROFILE_OF{$profile} ||= "AWS::CLI::Config::Profile"->_new($CONFIG->{$profile})
$CONFIG_PROFILE_OF{$profile} "AWS::CLI::Config::Profile"->_new($CONFIG->{$profile})
Truth table33%1/3
A ||= B
A= $CONFIG_PROFILE_OF{$profile}B= "AWS::CLI::Config::Profile"->_new($CONFIG->{$profile})
ABresult
1X1
011
000
92 2 return $CONFIG_PROFILE_OF{$profile};
93 }
94  
95 sub _base_dir {
96 2 ▶ ($^O eq 'MSWin32') ? $ENV{USERPROFILE} : $ENV{HOME};
Subroutine _base_dir: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 1 1 2
97 }
98  
99 sub _default_dir {
100 0 ▶ File::Spec->catdir(_base_dir(), '.aws');
Subroutine _default_dir: not called CC=1 SCAR=6.9
101 }
102  
103 sub _default_profile {
104 (exists $ENV{AWS_DEFAULT_PROFILE} && $ENV{AWS_DEFAULT_PROFILE})
105 ? $ENV{AWS_DEFAULT_PROFILE}
106 6 ▶ : $DEFAULT_PROFILE;
Subroutine _default_profile: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'AWS_DEFAULT_PROFILE'} && $ENV{'AWS_DEFAULT_PROFILE'} ? : 0 6 6
Condition33%1/3
exists $ENV{"AWS_DEFAULT_PROFILE"} and $ENV{"AWS_DEFAULT_PROFILE"}
!ll&&!rl&&r
600
MC/DC0%0/2
exists $ENV{"AWS_DEFAULT_PROFILE"} and $ENV{"AWS_DEFAULT_PROFILE"}
exists $ENV{"AWS_DEFAULT_PROFILE"} $ENV{"AWS_DEFAULT_PROFILE"}
Truth table33%1/3
A and B
A= exists $ENV{"AWS_DEFAULT_PROFILE"}B= $ENV{"AWS_DEFAULT_PROFILE"}
ABresult
0X0
100
111
107 }
108  
109 sub _parse {
110 1 ▶ my $file = shift;
Subroutine _parse: called CC=10 SCAR=23.2
111 1 ▶ my $profile = shift || _default_profile();
Condition33%1/3
shift() || _default_profile()
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || _default_profile()
shift() _default_profile()
Truth table33%1/3
A || B
A= shift()B= _default_profile()
ABresult
1X1
011
000
112  
113 1 my $hash = {};
114 1 my $nested = {};
115  
116 1 my $contents;
117 {
118 1 local $/ = undef;
119 1 open my $fh, '<', $file;
120 1 $contents = <$fh>;
121 1 close( $fh );
122 }
123  
124 1 foreach my $line (split /\n/, $contents) {
125 8 chomp $line;
126  
127 8 ▶ $profile = $1 if $line =~ /^\[(?:profile )?([\w]+)\]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\[(?:profile )?([\w]+)\]/ 2 6 8
128 8 my ($indent, $key, $value) = $line =~ /^(\s*)([\w]+)\s*=\s*(.*)/;
129  
130 8 ▶ next if !defined $key or $key eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "" 2 6 8
Condition66%2/3
not defined $key or $key eq ""
l!l&&r!l&&!r
206
MC/DC50%1/2
not defined $key or $key eq ""
not defined $key $key eq ""
Truth table66%2/3
not(A) or B
A= not defined $keyB= $key eq ""
ABresult
1X1
011
000
131  
132 6 ▶ if (length $indent) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $indent) { } 1 5 6
133 1 $nested->{$key} = $value;
134 }
135 else {
136 # Reset nested hash
137 5 ▶ $nested = {} if keys %{$nested};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$nested;} 0 5 5
138 5 ▶ $hash->{$profile}{$key} = ($key and $value) ? $value : $nested;
Branch: 1 branch
BranchTrueFalseTotal
$key && $value ? : 4 1 5
Condition66%2/3
$key and $value
!ll&&!rl&&r
014
MC/DC50%1/2
$key and $value
$key $value
Truth table66%2/3
A and B
A= $keyB= $value
ABresult
0X0
100
111
139 }
140 }
141  
142 1 return $hash;
143 }
144  
145 PROFILE: {
146 package AWS::CLI::Config::Profile;
147 5 ▶ use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 5 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 5 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 5 my @ACCESSORS;
152  
153 BEGIN {
154 5 ▶ @ACCESSORS = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
155 aws_access_key_id
156 aws_secret_access_key
157 aws_session_token
158 region
159 output
160 );
161 }
162  
163 5 ▶ use Object::Tiny @ACCESSORS;
Subroutine BEGIN: called CC=1 SCAR=0.0
164  
165 sub _new {
166 6 ▶ my $class = shift;
Subroutine _new: called CC=1 SCAR=0.0
167 6 my $data = shift;
168 6 return bless $data, $class;
169 }
170 }
171  
172 5 1;
173 __END__
↑ Summary Build »