Improving the config tests.
This commit is contained in:
parent
0cf0afe73e
commit
04008b578e
@ -7,5 +7,8 @@ WriteMakefile(
|
|||||||
test => { TESTS => 't/*.t' },
|
test => { TESTS => 't/*.t' },
|
||||||
PREREQ_PM => {
|
PREREQ_PM => {
|
||||||
'Term::ReadLine::Gnu' => 0,
|
'Term::ReadLine::Gnu' => 0,
|
||||||
|
'Test::Most' => 0,
|
||||||
|
'Test::MockModule' => 0,
|
||||||
|
'Test::MockObject' => 0,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -6,38 +6,8 @@ use strict;
|
|||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use JSON;
|
use JSON;
|
||||||
use Path::Tiny;
|
|
||||||
|
|
||||||
use DoctorAgustin::Logger;
|
use DoctorAgustin::Config;
|
||||||
|
|
||||||
|
my $config = DoctorAgustin::Config->new;
|
||||||
my $logger = DoctorAgustin::Logger->new;
|
say $config->telegram_token;
|
||||||
my $term = Term::ReadLine->new('DoctorAgustin');
|
|
||||||
|
|
||||||
if (! -e $CONFIG_FILE ) {
|
|
||||||
$logger->log_info(q(Config file not detected));
|
|
||||||
create_config_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! -f $CONFIG_FILE ) {
|
|
||||||
$logger->log_error(qq($CONFIG_FILE is not a plain file.));
|
|
||||||
die;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub create_config_file {
|
|
||||||
if (! -e $CONFIG_DIR ) {
|
|
||||||
$logger->log_info(qq(Creating $CONFIG_DIR));
|
|
||||||
eval {
|
|
||||||
path($CONFIG_DIR)->mkpath;
|
|
||||||
};
|
|
||||||
if ($@) {
|
|
||||||
$logger->log_error(qq(Unable to create directory $CONFIG_DIR: $@));
|
|
||||||
die;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
my $token = $term->readline('Telegram token:');
|
|
||||||
my $config_contents = { token => $token };
|
|
||||||
$config_contents = encode_json ($config_contents);
|
|
||||||
|
|
||||||
path($CONFIG_FILE)->spew_utf8($config_contents);
|
|
||||||
}
|
|
||||||
|
@ -9,26 +9,40 @@ use Term::ReadLine;
|
|||||||
|
|
||||||
use Const::Fast;
|
use Const::Fast;
|
||||||
use JSON;
|
use JSON;
|
||||||
|
use Path::Tiny;
|
||||||
|
|
||||||
sub HOME { $ENV{HOME} }
|
use DoctorAgustin::Logger;
|
||||||
sub CONFIG_DIR {"@{[HOME]}/.config/doctoragustin" }
|
|
||||||
|
sub HOME { $ENV{HOME} }
|
||||||
|
sub CONFIG_DIR { "@{[HOME]}/.config/doctoragustin" }
|
||||||
sub CONFIG_FILE { "@{[CONFIG_DIR]}/config.json" }
|
sub CONFIG_FILE { "@{[CONFIG_DIR]}/config.json" }
|
||||||
|
|
||||||
sub new {
|
sub new {
|
||||||
my $class = shift;
|
my $class = shift;
|
||||||
my $self = bless {}, $class;
|
my $self = bless {}, $class;
|
||||||
$self->_create_config_file_if_not_exists;
|
$self->_create_config_file_if_not_exists;
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub config {
|
sub logger {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
if (!defined $self->{config}) {
|
if ( !defined $self->{logger} ) {
|
||||||
if (! -f CONFIG_FILE ) {
|
my $logger = DoctorAgustin::Logger->new;
|
||||||
$logger->log_error(qq(@{CONFIG_FILE]} is not a plain file, unable to read config.));
|
$self->{logger} = $logger;
|
||||||
die;
|
|
||||||
}
|
}
|
||||||
my $config = decode_json(path(CONFIG_FILE)->slurp_utf8)
|
return $self->{logger};
|
||||||
|
}
|
||||||
|
|
||||||
|
sub _config {
|
||||||
|
my $self = shift;
|
||||||
|
if ( !defined $self->{config} ) {
|
||||||
|
if ( !-f CONFIG_FILE ) {
|
||||||
|
$self->logger->log_error(
|
||||||
|
qq(@{[CONFIG_FILE]} is not a plain file, unable to read config.)
|
||||||
|
);
|
||||||
|
die;
|
||||||
|
}
|
||||||
|
my $config = decode_json( path(CONFIG_FILE)->slurp_utf8 );
|
||||||
$self->{config} = $config;
|
$self->{config} = $config;
|
||||||
}
|
}
|
||||||
return $self->{config};
|
return $self->{config};
|
||||||
@ -36,8 +50,8 @@ sub config {
|
|||||||
|
|
||||||
sub telegram_token {
|
sub telegram_token {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
if (!defined $self->{telegram_token}) {
|
if ( !defined $self->{telegram_token} ) {
|
||||||
my $config = $self->config;
|
my $config = $self->_config;
|
||||||
$self->{telegram_token} = $config->{telegram_token};
|
$self->{telegram_token} = $config->{telegram_token};
|
||||||
}
|
}
|
||||||
return $self->{telegram_token};
|
return $self->{telegram_token};
|
||||||
@ -45,28 +59,30 @@ sub telegram_token {
|
|||||||
|
|
||||||
sub _create_config_file_if_not_exists {
|
sub _create_config_file_if_not_exists {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
if (! -e CONFIG_FILE ) {
|
if ( !-e CONFIG_FILE ) {
|
||||||
$logger->log_info(q(Config file not detected));
|
$self->logger->log_info(q(Config file not detected));
|
||||||
$self->create_config_file;
|
$self->_create_config_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _create_config_file {
|
sub _create_config_file {
|
||||||
if (! -e CONFIG_DIR ) {
|
my $self = shift;
|
||||||
|
my $logger = $self->logger;
|
||||||
|
if ( !-e CONFIG_DIR ) {
|
||||||
$logger->log_info(qq(Creating @{[CONFIG_DIR]}));
|
$logger->log_info(qq(Creating @{[CONFIG_DIR]}));
|
||||||
eval {
|
eval { path(CONFIG_DIR)->mkpath; };
|
||||||
path(CONFIG_DIR)->mkpath;
|
|
||||||
};
|
|
||||||
if ($@) {
|
if ($@) {
|
||||||
$logger->log_error(qq(Unable to create directory @{[CONFIG_DIR]}: $@));
|
$logger->log_error(
|
||||||
|
qq(Unable to create directory @{[CONFIG_DIR]}: $@));
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
my $token = $term->readline('Telegram token:');
|
my $term = Term::ReadLine->new('Doctor Agustín');
|
||||||
my $config_contents = { token => $token };
|
my $token = $term->readline('Telegram token:');
|
||||||
$config_contents = encode_json ($config_contents);
|
my $config_contents = { telegram_token => $token };
|
||||||
|
$config_contents = encode_json($config_contents);
|
||||||
|
|
||||||
path($CONFIG_FILE)->spew_utf8($config_contents);
|
path(CONFIG_FILE)->spew_utf8($config_contents);
|
||||||
}
|
}
|
||||||
1;
|
1;
|
||||||
|
Loading…
Reference in New Issue
Block a user