File Coverage

blib/lib/AnyEvent/MP/Config.pm
Criterion Covered Total %
statement 16 35 45.7
branch 2 16 12.5
condition n/a
subroutine 7 10 70.0
pod 0 4 0.0
total 25 65 38.4


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             AnyEvent::MP::Config - configuration handling
4              
5             =head1 SYNOPSIS
6              
7             # see the "aemp" command line utility
8              
9             =head1 DESCRIPTION
10              
11             Move along please, nothing to see here at the moment.
12              
13             =cut
14              
15             package AnyEvent::MP::Config;
16              
17 1     1   663 use common::sense;
  1         2  
  1         7  
18              
19 1     1   51 use Carp ();
  1         2  
  1         16  
20 1     1   1936 use AnyEvent ();
  1         5904  
  1         27  
21 1     1   10 use JSON::XS ();
  1         2  
  1         907  
22              
23             our $CONFIG_FILE = exists $ENV{PERL_ANYEVENT_MP_RC} ? $ENV{PERL_ANYEVENT_MP_RC}
24             : exists $ENV{HOME} ? "$ENV{HOME}/.perl-anyevent-mp"
25             : "$ENV{APPDATA}/perl-anyevent-mp";
26              
27             our %CFG;
28              
29             sub load {
30 1 50   1 0 46 if (open my $fh, "<:raw", $CONFIG_FILE) {
31 0 0       0 return if eval {
32 0         0 local $/;
33 0         0 %CFG = %{ JSON::XS->new->utf8->relaxed->decode (scalar <$fh>) };
  0         0  
34 0         0 1
35             };
36             }
37              
38             %CFG = (
39 1         6 version => 1,
40             );
41             }
42              
43             sub save {
44 1 50   1 0   return unless delete $CFG{dirty};
45              
46 0 0         open my $fh, ">:raw", "$CONFIG_FILE~new~"
47             or Carp::croak "$CONFIG_FILE~new~: $!";
48              
49 0 0         syswrite $fh, JSON::XS->new->pretty->utf8->encode (\%CFG) . "\n"
50             or Carp::croak "$CONFIG_FILE~new~: $!";
51              
52 0 0         close $fh
53             or Carp::croak "$CONFIG_FILE~new~: $!";
54              
55 0           unlink "$CONFIG_FILE~";
56 0           link $CONFIG_FILE, "$CONFIG_FILE~";
57 0 0         rename "$CONFIG_FILE~new~", $CONFIG_FILE
58             or Carp::croak "$CONFIG_FILE: $!";
59             }
60              
61             sub config {
62 0     0 0   \%CFG
63             }
64              
65             sub _find_profile($);
66             sub _find_profile($) {
67 0     0     my ($name) = @_;
68              
69 0 0         if (defined $name) {
70 0           my $profile = $CFG{profile}{$name};
71 0           return _find_profile $profile->{parent}, %$profile;
72             } else {
73 0           return %CFG;
74             }
75             }
76              
77             sub find_profile($;%) {
78 0     0 0   my ($name, %kv) = @_;
79              
80             +{
81 0           monitor_timeout => 30,
82             connect_interval => 2,
83             framing_format => [qw(json storable)], # framing types we offer and accept, in order of preference
84             auth_offer => [qw(tls_md6_64_256 hmac_md6_64_256)], # what we will send
85             auth_accept => [qw(tls_md6_64_256 hmac_md6_64_256 tls_anon cleartext)], # what we accept
86             %kv,
87             _find_profile $name,
88             }
89             }
90              
91             load;
92 1     1   670 END { save }
93              
94             =head1 SEE ALSO
95              
96             L<AnyEvent::MP>.
97              
98             =head1 AUTHOR
99              
100             Marc Lehmann <schmorp@schmorp.de>
101             http://home.schmorp.de/
102              
103             =cut
104              
105             1
106