MToken

 view release on metacpan or  search on metacpan

lib/MToken/Config.pm  view on Meta::CPAN


Returns all configuration pairs - key and value

=item B<is_loaded>

    print $self->is_loaded ? 'loaded' : 'not loaded';

Returns status of local config

=item B<set>

    $config->set( 'key', $value );

Set new value for key. Returns status of the operation

=item B<save>

    $config->save;

Save current configuration to local_file and returns status of the operation

=back

=head1 HISTORY

See C<Changes> file

=head1 AUTHOR

Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright (C) 1998-2021 D&D Corporation. All Rights Reserved

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See C<LICENSE> file and L<https://dev.perl.org/licenses/>

=cut

use Carp;
use File::HomeDir;
use Config::General;
use Try::Tiny;
use Cwd;
use File::Spec;
use CTK::Util qw/preparedir/;
use MToken::Const qw/ :GENERAL :MATH /;

use vars qw/$VERSION/;
$VERSION = '1.03';

use constant {
    ALLOWED_KEYS        => [qw/
            token
            server_url
            gpgbin opensslbin
            fingerprint
        /],
};

sub new {
    my $class   = shift;
    my %args    = (@_);
    my $is_loaded = 0;

    my $config_file = $args{file} || $args{config_file} || $args{device_file}
       || File::Spec->catfile(cwd(), DIR_PRIVATE, DEVICE_CONF_FILE);

    # Load device config
    my %dev = _loadconfig($config_file);
    my $name = $dev{token} || $dev{token_name} || $dev{name};
    $is_loaded = 1 if $name;


    # Get path of local config
    $name ||= $args{name} || "noname";
    $name =~ s/\s+//g;
    $name =~ s/[^a-z0-9]//g;
    $name ||= "noname";
    my $local_dir = File::Spec->catdir(File::HomeDir->my_data(), PROJECTNAMEL);
    my $local_file = File::Spec->catfile($local_dir, sprintf("%s.conf", $name));
    my %lkl = ();
    if (-f $local_file) { # Ok! File exists, try load local config
        %lkl = _loadconfig($local_file);
        while (my ($k,$v) = each %lkl) {
            $dev{$k} //= $v if defined $v;
        }
    }

    # Set data
    my %cfg = (
        name                => $name,
        is_loaded           => $is_loaded,
        device_config_file  => $config_file,
        local_config_dir    => $local_dir,
        local_config_file   => $local_file,
        _config             => {%dev},
    );

    my $self = bless { %cfg }, $class;
    return $self;
}
sub save {
    my $self = shift;
    if (!$self->{name} || $self->{name} eq "noname") {
        carp("Can't use nonamed devices");
        return FALSE;
    }

    my %svh = ();
    foreach my $k (@{(ALLOWED_KEYS)}) {
        my $v = $self->get($k);
        $svh{$k} = $v if defined $v;
    }
    preparedir($self->{local_config_dir}, 0755) or do {
        carp(sprintf("Can't prepare directory %s", $self->{local_config_dir}));



( run in 1.008 second using v1.01-cache-2.11-cpan-df04353d9ac )