File Coverage

lib/App/gh/Config.pm
Criterion Covered Total %
statement 15 46 32.6
branch 0 16 0.0
condition n/a
subroutine 5 12 41.6
pod 0 7 0.0
total 20 81 24.6


line stmt bran cond sub pod time code
1             package App::gh::Config;
2 2     2   16 use warnings;
  2         4  
  2         52  
3 2     2   10 use strict;
  2         2  
  2         52  
4 2     2   2474 use File::HomeDir ();
  2         15030  
  2         49  
5 2     2   20 use File::Spec;
  2         4  
  2         50  
6 2     2   11 use File::Basename qw(dirname);
  2         3  
  2         1311  
7              
8              
9              
10              
11             my %_parse_memoize;
12             # XXX: use Config::Tiny to parse ini format config.
13             sub parse {
14 0     0 0   my ( $class, $file ) = @_;
15              
16             # Return cached result.
17 0           $file = File::Spec->rel2abs($file);
18 0 0         return $_parse_memoize{$file} if exists $_parse_memoize{$file};
19              
20 0           my %config;
21 0           for my $line (split "\n", qx(git config --list -f '$file')) {
22             # $line = foo.bar.baz=value
23 0 0         if (my ($key, $value) = ($line =~ m/^([^=]+)=(.*)/)) {
24 0           my $h = \%config;
25 0 0         if ($key eq 'include.path') {
26 0 0         my $path = File::Spec->file_name_is_absolute($value) ? $value : File::Spec->rel2abs($value, dirname($file));
27 0           %config = (%config, %{ $class->parse($path) });
  0            
28             # Uncomment this to get rid of "include.path" in %config
29             #next;
30             }
31 0           my @keys = split /\./, $key;
32 0 0         next unless @keys;
33             # Create empty hashref.
34             # %config = (foo => {bar => ($h = {})})
35 0           for (@keys[0..$#keys-1]) {
36 0 0         $h->{$_} = {} unless exists $h->{$_};
37 0           $h = $h->{$_};
38             }
39             # $config{foo}{bar}{baz} = $value;
40 0           $h->{$keys[-1]} = $value;
41             }
42             }
43              
44             # Cache result not to invoke 'git' command frequently.
45 0           $_parse_memoize{$file} = \%config;
46 0           return \%config;
47             }
48              
49              
50             sub current {
51 0     0 0   my $class = shift;
52 0           my $path = File::Spec->join(".git","config");
53 0 0         return unless -e $path;
54              
55             # TODO: prevent error
56 0           return $class->parse( $path );
57             }
58              
59             # when command trying to read config, we should let it die, and provide another
60             # method to check github id and token.
61             # XXX: abandoned, since we are using Net::GitHub V3
62 0     0 0   sub github_token { return $_[0]->global()->{github}->{token}; }
63              
64 0     0 0   sub github_password { return $_[0]->global()->{github}->{password}; }
65              
66             # Auth with OAuth
67 0     0 0   sub github_access_token { return $_[0]->global()->{github}->{access_token}; }
68              
69 0     0 0   sub github_id { return $_[0]->global()->{github}->{user}; }
70              
71             sub global {
72 0     0 0   my $class = shift;
73 0           my $path = File::Spec->join(File::HomeDir->my_home, '.gitconfig');
74 0 0         return unless -e $path;
75 0           return $class->parse( $path );
76             }
77              
78             1;