Improving the config tests.

This commit is contained in:
sergiotarxz 2022-02-04 21:44:00 +01:00
parent 0cf0afe73e
commit 04008b578e
3 changed files with 45 additions and 56 deletions

View File

@ -7,5 +7,8 @@ WriteMakefile(
test => { TESTS => 't/*.t' },
PREREQ_PM => {
'Term::ReadLine::Gnu' => 0,
'Test::Most' => 0,
'Test::MockModule' => 0,
'Test::MockObject' => 0,
},
);

View File

@ -6,38 +6,8 @@ use strict;
use warnings;
use JSON;
use Path::Tiny;
use DoctorAgustin::Logger;
use DoctorAgustin::Config;
my $logger = DoctorAgustin::Logger->new;
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);
}
my $config = DoctorAgustin::Config->new;
say $config->telegram_token;

View File

@ -9,26 +9,40 @@ use Term::ReadLine;
use Const::Fast;
use JSON;
use Path::Tiny;
sub HOME { $ENV{HOME} }
sub CONFIG_DIR {"@{[HOME]}/.config/doctoragustin" }
use DoctorAgustin::Logger;
sub HOME { $ENV{HOME} }
sub CONFIG_DIR { "@{[HOME]}/.config/doctoragustin" }
sub CONFIG_FILE { "@{[CONFIG_DIR]}/config.json" }
sub new {
my $class = shift;
my $self = bless {}, $class;
my $self = bless {}, $class;
$self->_create_config_file_if_not_exists;
return $self;
}
sub config {
sub logger {
my $self = shift;
if (!defined $self->{config}) {
if (! -f CONFIG_FILE ) {
$logger->log_error(qq(@{CONFIG_FILE]} is not a plain file, unable to read config.));
die;
if ( !defined $self->{logger} ) {
my $logger = DoctorAgustin::Logger->new;
$self->{logger} = $logger;
}
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;
}
return $self->{config};
@ -36,8 +50,8 @@ sub config {
sub telegram_token {
my $self = shift;
if (!defined $self->{telegram_token}) {
my $config = $self->config;
if ( !defined $self->{telegram_token} ) {
my $config = $self->_config;
$self->{telegram_token} = $config->{telegram_token};
}
return $self->{telegram_token};
@ -45,28 +59,30 @@ sub telegram_token {
sub _create_config_file_if_not_exists {
my $self = shift;
if (! -e CONFIG_FILE ) {
$logger->log_info(q(Config file not detected));
$self->create_config_file;
if ( !-e CONFIG_FILE ) {
$self->logger->log_info(q(Config file not detected));
$self->_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]}));
eval {
path(CONFIG_DIR)->mkpath;
};
eval { path(CONFIG_DIR)->mkpath; };
if ($@) {
$logger->log_error(qq(Unable to create directory @{[CONFIG_DIR]}: $@));
$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);
my $term = Term::ReadLine->new('Doctor Agustín');
my $token = $term->readline('Telegram token:');
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;