feat: perltidy

This commit is contained in:
sergiotarxz 2020-08-15 18:45:57 +02:00
parent c3134f71cf
commit e535987d5b

View File

@ -2,90 +2,92 @@ use strict;
use warnings; use warnings;
package Kernel::Config { package Kernel::Config {
sub new {
my $class = shift;
my %params = @_;
my $self = bless {}, $class;
my $file = $params{file};
$self->_checkIsPlainFileAndReadable( file => $file );
$self->_readConfigFile( file => $file );
$self;
}
sub _checkIsPlainFileAndReadable { sub new {
my $self = shift; my $class = shift;
my %params = @_; my %params = @_;
my $file = $params{file}; my $self = bless {}, $class;
die "No file param" unless defined $file; my $file = $params{file};
die "No such file $file" unless -e $file; $self->_checkIsPlainFileAndReadable( file => $file );
die "No plain file $file" unless -f $file; $self->_readConfigFile( file => $file );
die "Cannot read $file" unless -r $file; $self;
} }
sub _readConfigFile { sub _checkIsPlainFileAndReadable {
my $self = shift; my $self = shift;
my %params = @_; my %params = @_;
my $file = $params{file}; my $file = $params{file};
open my $fh, '<', $file or die "Cannot open $file for read"; die "No file param" unless defined $file;
my $lines = [ <$fh> ]; die "No such file $file" unless -e $file;
$lines = [ grep { !(/#/ || /^\s*$/) } @$lines]; die "No plain file $file" unless -f $file;
my $hash_config = $self->_parseConfigLinesToHash( lines => $lines ); die "Cannot read $file" unless -r $file;
$self->{hash_config} = $hash_config; }
}
sub _parseConfigLinesToHash { sub _readConfigFile {
my $self = shift; my $self = shift;
my %params = @_; my %params = @_;
my $lines = $params{lines}; my $file = $params{file};
my $hash_config = { map { chomp; split qr/=/ } @$lines }; open my $fh, '<', $file or die "Cannot open $file for read";
return $hash_config; my $lines = [<$fh>];
} $lines = [ grep { !( /#/ || /^\s*$/ ) } @$lines ];
my $hash_config = $self->_parseConfigLinesToHash( lines => $lines );
$self->{hash_config} = $hash_config;
}
sub hashConfig { sub _parseConfigLinesToHash {
my $self = shift; my $self = shift;
$self->{hash_config} = $_[1] if defined $_[1]; my %params = @_;
return $self->{hash_config}; my $lines = $params{lines};
} my $hash_config = { map { chomp; split qr/=/ } @$lines };
return $hash_config;
}
sub merge { sub hashConfig {
my $self = shift; my $self = shift;
my %params = @_; $self->{hash_config} = $_[1] if defined $_[1];
my $remote_kernel_config = $params{kernel_config}; return $self->{hash_config};
my $remote_hash_config = $remote_kernel_config->hashConfig; }
my $hash_config = $self->hashConfig;
my $keys = [ keys %$hash_config, keys %$remote_hash_config ];
my $new_hash_config = {
map {
$self->_optionSelectorMergerConfigFirst(
key => $_,
hash_config => $hash_config,
remote_hash_config => $remote_hash_config
)
} @$keys
};
$self->hashConfig( $new_hash_config );
$self;
}
sub _optionSelectorMergerConfigFirst { sub merge {
my $self = shift; my $self = shift;
my %params = @_; my %params = @_;
my $key = $params{key}; my $remote_kernel_config = $params{kernel_config};
my $hash_config = $params{hash_config}; my $remote_hash_config = $remote_kernel_config->hashConfig;
my $remote_hash_config = $params{remote_hash_config}; my $hash_config = $self->hashConfig;
if ( defined $hash_config->{$key} && $hash_config->{$key} !~ /^\s*$/ ) { my $keys = [ keys %$hash_config, keys %$remote_hash_config ];
return ( $key => $hash_config->{$key} ) my $new_hash_config = {
} else { map {
return ( $key => ($remote_hash_config->{$key} // "")); $self->_optionSelectorMergerConfigFirst(
} key => $_,
} hash_config => $hash_config,
remote_hash_config => $remote_hash_config
)
} @$keys
};
$self->hashConfig($new_hash_config);
$self;
}
sub plainConfig { sub _optionSelectorMergerConfigFirst {
my $self = shift; my $self = shift;
my $hash_config = $self->hashConfig; my %params = @_;
my $plain_config = join( "\n", ( map { "$_=$hash_config->{$_}" } keys %$hash_config ) ); my $key = $params{key};
$plain_config .= "\n"; my $hash_config = $params{hash_config};
return $plain_config; my $remote_hash_config = $params{remote_hash_config};
} if ( defined $hash_config->{$key} && $hash_config->{$key} !~ /^\s*$/ ) {
return ( $key => $hash_config->{$key} );
} else {
return ( $key => ( $remote_hash_config->{$key} // "" ) );
}
}
sub plainConfig {
my $self = shift;
my $hash_config = $self->hashConfig;
my $plain_config = join( "\n", ( map { "$_=$hash_config->{$_}" } keys %$hash_config ) );
$plain_config .= "\n";
return $plain_config;
}
} }
print Kernel::Config->new( file => $ARGV[0] )->merge( kernel_config => Kernel::Config->new( file => $ARGV[1] ))->plainConfig; print Kernel::Config->new( file => $ARGV[0] )->merge( kernel_config => Kernel::Config->new( file => $ARGV[1] ) )
->plainConfig;