File Coverage

blib/lib/AnyEvent/MP/Config.pm
Criterion Covered Total %
statement 16 42 38.1
branch 2 20 10.0
condition n/a
subroutine 7 10 70.0
pod 0 4 0.0
total 25 76 32.8


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   771 use common::sense;
  1         9  
  1         4  
18              
19 1     1   37 use Carp ();
  1         1  
  1         10  
20 1     1   707 use AnyEvent ();
  1         3835  
  1         18  
21 1     1   511 use JSON::XS ();
  1         1943  
  1         591  
22              
23             our $VERSION = '2.02';
24              
25             our $CONFIG_FILE = exists $ENV{PERL_ANYEVENT_MP_RC} ? $ENV{PERL_ANYEVENT_MP_RC}
26             : exists $ENV{HOME} ? "$ENV{HOME}/.perl-anyevent-mp"
27             : "$ENV{APPDATA}/perl-anyevent-mp";
28              
29             our %CFG;
30              
31             sub load {
32 1 50   1 0 47 if (open my $fh, "<:raw", $CONFIG_FILE) {
33 0 0       0 return if eval {
34 0         0 local $/;
35 0         0 %CFG = %{ JSON::XS->new->utf8->relaxed->decode (scalar <$fh>) };
  0         0  
36 0         0 1
37             };
38             }
39              
40             %CFG = (
41 1         4 version => 1,
42             );
43             }
44              
45             sub save {
46 1 50   1 0   return unless delete $CFG{dirty};
47              
48 0 0         open my $fh, ">:raw", "$CONFIG_FILE~new~"
49             or Carp::croak "$CONFIG_FILE~new~: $!";
50              
51 0 0         syswrite $fh, JSON::XS->new->pretty->utf8->encode (\%CFG) . "\n"
52             or Carp::croak "$CONFIG_FILE~new~: $!";
53              
54 0 0         close $fh
55             or Carp::croak "$CONFIG_FILE~new~: $!";
56              
57 0           unlink "$CONFIG_FILE~";
58 0           link $CONFIG_FILE, "$CONFIG_FILE~";
59 0 0         rename "$CONFIG_FILE~new~", $CONFIG_FILE
60             or Carp::croak "$CONFIG_FILE: $!";
61             }
62              
63             sub config {
64 0     0 0   \%CFG
65             }
66              
67             sub _find_profile($);
68             sub _find_profile($) {
69 0     0     my ($name) = @_;
70              
71 0 0         if (defined $name) {
72 0           my $profile = $CFG{profile}{$name};
73 0           return _find_profile $profile->{parent}, %$profile;
74             } else {
75 0           return %CFG;
76             }
77             }
78              
79             sub find_profile($;%) {
80 0     0 0   my ($name, %kv) = @_;
81              
82 0           my $norc = delete $kv{norc};
83 0           my $force = delete $kv{force};
84              
85 0           %kv = (
86             monitor_timeout => 30,
87             connect_interval => 2,
88             framing_format => [qw(cbor json storable)], # framing types we offer and accept, in order of preference
89             auth_offer => [qw(tls_sha3_512 hmac_sha3_512)], # what we will send
90             auth_accept => [qw(tls_sha3_512 hmac_sha3_512 tls_anon cleartext)], # what we accept
91             %kv,
92             );
93              
94 0 0         unless ($norc) {
95 0 0         if ($force) {
96 0           %kv = (_find_profile $name, %kv);
97             } else {
98 0           %kv = (%kv, _find_profile $name);
99             }
100             }
101              
102 0           \%kv
103             }
104              
105             load;
106 1     1   204 END { save }
107              
108             =head1 SEE ALSO
109              
110             L.
111              
112             =head1 AUTHOR
113              
114             Marc Lehmann
115             http://home.schmorp.de/
116              
117             =cut
118              
119             1
120