| 1 |
root |
1.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 |
root |
1.2 |
Move along please, nothing to see here at the moment. |
| 12 |
root |
1.1 |
|
| 13 |
|
|
=cut |
| 14 |
|
|
|
| 15 |
|
|
package AnyEvent::MP::Config; |
| 16 |
|
|
|
| 17 |
|
|
use common::sense; |
| 18 |
|
|
|
| 19 |
|
|
use Carp (); |
| 20 |
|
|
use AnyEvent (); |
| 21 |
|
|
use JSON::XS (); |
| 22 |
|
|
|
| 23 |
|
|
our $CONFIG_FILE = $ENV{HOME} ? "$ENV{HOME}/.perl-anyevent-mp" |
| 24 |
|
|
: "$ENV{APPDATA}/perl-anyevent-mp"; |
| 25 |
|
|
|
| 26 |
|
|
our %CFG; |
| 27 |
|
|
|
| 28 |
|
|
sub load { |
| 29 |
|
|
if (open my $fh, "<:raw", $CONFIG_FILE) { |
| 30 |
|
|
return if eval { |
| 31 |
|
|
local $/; |
| 32 |
|
|
%CFG = %{ JSON::XS->new->utf8->relaxed->decode (scalar <$fh>) }; |
| 33 |
|
|
1 |
| 34 |
|
|
}; |
| 35 |
|
|
} |
| 36 |
|
|
|
| 37 |
|
|
%CFG = ( |
| 38 |
|
|
version => 1, |
| 39 |
|
|
dirty => 1, |
| 40 |
|
|
); |
| 41 |
|
|
} |
| 42 |
|
|
|
| 43 |
|
|
sub save { |
| 44 |
|
|
return unless delete $CFG{dirty}; |
| 45 |
|
|
|
| 46 |
|
|
open my $fh, ">:raw", "$CONFIG_FILE~new~" |
| 47 |
|
|
or Carp::croak "$CONFIG_FILE~new~: $!"; |
| 48 |
|
|
|
| 49 |
|
|
syswrite $fh, JSON::XS->new->pretty->utf8->encode (\%CFG) . "\n" |
| 50 |
|
|
or Carp::croak "$CONFIG_FILE~new~: $!"; |
| 51 |
|
|
|
| 52 |
|
|
close $fh |
| 53 |
|
|
or Carp::croak "$CONFIG_FILE~new~: $!"; |
| 54 |
|
|
|
| 55 |
|
|
unlink "$CONFIG_FILE~"; |
| 56 |
|
|
link $CONFIG_FILE, "$CONFIG_FILE~"; |
| 57 |
|
|
rename "$CONFIG_FILE~new~", $CONFIG_FILE |
| 58 |
|
|
or Carp::croak "$CONFIG_FILE: $!"; |
| 59 |
|
|
} |
| 60 |
|
|
|
| 61 |
root |
1.3 |
sub config { |
| 62 |
root |
1.2 |
\%CFG |
| 63 |
|
|
} |
| 64 |
|
|
|
| 65 |
root |
1.4 |
sub _find_profile($) { |
| 66 |
|
|
my ($name) = @_; |
| 67 |
|
|
|
| 68 |
|
|
if (defined $name) { |
| 69 |
|
|
my $profile = $CFG{profile}{$name}; |
| 70 |
|
|
return _find_profile ($profile->{parent}), %$profile; |
| 71 |
|
|
} else { |
| 72 |
|
|
return %CFG; |
| 73 |
|
|
} |
| 74 |
|
|
} |
| 75 |
|
|
|
| 76 |
|
|
sub find_profile($;%) { |
| 77 |
|
|
my ($name, %kv) = @_; |
| 78 |
|
|
|
| 79 |
root |
1.3 |
+{ |
| 80 |
root |
1.4 |
%kv, |
| 81 |
|
|
_find_profile $name, |
| 82 |
root |
1.3 |
} |
| 83 |
|
|
} |
| 84 |
|
|
|
| 85 |
root |
1.1 |
load; |
| 86 |
|
|
END { save } |
| 87 |
|
|
|
| 88 |
|
|
=head1 SEE ALSO |
| 89 |
|
|
|
| 90 |
|
|
L<AnyEvent::MP>. |
| 91 |
|
|
|
| 92 |
|
|
=head1 AUTHOR |
| 93 |
|
|
|
| 94 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 95 |
|
|
http://home.schmorp.de/ |
| 96 |
|
|
|
| 97 |
|
|
=cut |
| 98 |
|
|
|
| 99 |
|
|
1 |
| 100 |
|
|
|