Skip to content

Commit

Permalink
feat: add update and use sub for do_install/remove
Browse files Browse the repository at this point in the history
  • Loading branch information
rick-gnous authored and d0p1s4m4 committed Jan 24, 2022
1 parent deaa444 commit 3c5acc2
Showing 1 changed file with 61 additions and 33 deletions.
94 changes: 61 additions & 33 deletions barista
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,44 @@ sub get_compression_algorithm {
return undef;
}

sub do_install {
for (@ARGV) {
sub remove_pkg {
my @tmp = @_;
for (@tmp) {
my $pkgname = $_;
my $installed_version = is_package_installed($pkgname);
if ($installed_version) {
print "$pkgname (v$installed_version) is already installed\n";
if (!$installed_version) {
print "$pkgname isn't installed on your system\n";
next;
}

open(my $fd, "<", "$ADM_DIR/packages/$pkgname.list");
while (<$fd>) {
chomp($_);
my $path = "$SYSROOT/$_";
if (-d $path) {
rmdir($path);
} else {
unlink($path);
}
}
close($fd);
unlink("$ADM_DIR/packages/$pkgname.list");

open($fd, "<", "$ADM_DIR/installed");
my @installed_packages = <$fd>;
close($fd);
open($fd, ">", "$ADM_DIR/installed");
foreach my $installed ( @installed_packages ) {
print $fd $installed unless ( $installed =~ /^$pkgname:/);
}
close($fd);
}
}

sub install_pkg {
my @tmp = @_;
for (@tmp) {
my $pkgname = $_;
my ($pkgversion, $pkgurl) = search_package($pkgname);
if (!$pkgversion) {
print "$pkgname not found in repositories\n";
Expand Down Expand Up @@ -125,43 +154,42 @@ sub do_install {
}
}

sub do_install {
install_pkg(@ARGV);
}

sub do_remove {
for (@ARGV) {
my $pkgname = $_;
my $installed_version = is_package_installed($pkgname);
if (!$installed_version) {
print "$pkgname isn't installed on your system\n";
next;
}
remove_pkg(@ARGV);
}

open(my $fd, "<", "$ADM_DIR/packages/$pkgname.list");
while (<$fd>) {
chomp($_);
my $path = "$SYSROOT/$_";
if (-d $path) {
rmdir($path);
} else {
unlink($path);
}
sub do_update {
print Dumper(@ARGV);
my @pkginstalled;
my @updatepkg;
open (my $fd, "<", "$ADM_DIR/installed");
while (<$fd>) {
chomp($_);
my ($pkgname, $pkgversion) = split(/:/, $_);
my ($newversion, undef) = search_package($pkgname);
chomp($pkgversion);
chomp($newversion);
if ($pkgversion && $newversion > $pkgversion) {
push(@updatepkg, $pkgname);
}
close($fd);
unlink("$ADM_DIR/packages/$pkgname.list");
}
close($fd);

open($fd, "<", "$ADM_DIR/installed");
my @installed_packages = <$fd>;
close($fd);
open($fd, ">", "$ADM_DIR/installed");
foreach my $installed ( @installed_packages ) {
print $fd $installed unless ( $installed =~ /^$pkgname:/);
if (@updatepkg) {
print "update ? [o/N]";
my $userchoice = <STDIN>;
chomp($userchoice);
if (lc $userchoice =~ "o") {
remove_pkg(@updatepkg);
install_pkg(@updatepkg);
}
close($fd);
}
}

sub do_update {
print Dumper(@ARGV);
}

sub do_list_installed {
my ($pkgname) = @_;
if (-f "$ADM_DIR/installed") {
Expand Down

0 comments on commit 3c5acc2

Please sign in to comment.