From 7a4f292d800902aa8dab3d62c15b7878425c2548 Mon Sep 17 00:00:00 2001 From: htrgouvea Date: Tue, 18 May 2021 23:29:36 -0300 Subject: [PATCH] optimizing the code by removing the varius ifs/else's and setting the value on the last moment based on a hash --- lib/Nipe/Utils/Install.pm | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/lib/Nipe/Utils/Install.pm b/lib/Nipe/Utils/Install.pm index 0b6e616..1b2ff09 100644 --- a/lib/Nipe/Utils/Install.pm +++ b/lib/Nipe/Utils/Install.pm @@ -6,36 +6,27 @@ package Nipe::Utils::Install { sub new { my %device = Nipe::Utils::Device -> new(); my $stopTor = "systemctl stop tor"; + + my %install = ( + "debian" => "apt-get install -y tor iptables", + "fedora" => "dnf install -y tor iptables", + "centos" => "yum -y install epel-release tor iptables", + "void" => "xbps-install -y tor iptables", + "arch" => "pacman -S --noconfirm tor iptables" + ); - if ($device{distribution} eq "debian") { - system ("apt-get install -y tor iptables"); - } - - elsif ($device{distribution} eq "fedora") { - system ("dnf install -y tor iptables"); - } - - elsif ($device{distribution} eq "centos") { - system ("yum -y install epel-release tor iptables"); - } - - elsif ($device{distribution} eq "void") { - system ("xbps-install -y tor iptables"); + if ($device{distribution} eq "void") { $stopTor = "sv stop tor > /dev/null"; } - else { - system ("pacman -S --noconfirm tor iptables"); - } - if (-e "/etc/init.d/tor") { $stopTor = "/etc/init.d/tor stop > /dev/null"; } - system ($stopTor); + system("$install{$device{distribution}} && $stopTor"); return 1; } } -1; +1; \ No newline at end of file