-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacnet
executable file
·74 lines (63 loc) · 1.68 KB
/
pacnet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/perl
use JSON::XS;
use Term::ANSIColor;
use Getopt::Long;
use autodie;
use 5.012;
my @packages; #here be database
my $dburl = 'http://pacnet.karbownicki.com/api/packages/';
my $dbfile = $ENV{'HOME'} . '/.config/pacnet.db';
sub printpackage {
my $p = shift;
say colored('*', 'green'), ' ',
$p->{'category__name'} .'/',
colored($p->{'name'}, 'bold'), "\n ",
colored("Version: \t", 'green'), $p->{'version'}, "\n ",
colored("Description:\t", 'green'), $p->{'description'}, "\n";
}
sub packagestring {
my $p = shift;
return $p->{'category__name'} . '/' . $p->{'name'};
}
sub getdb {
require LWP::Simple;
unless(LWP::Simple::getstore($dburl, $dbfile) == 200) {
say "Failed downloading the package database";
exit 1;
}
}
sub readdb {
getdb unless -f $dbfile;
open my $db, '<', $dbfile;
my $json = do { local $/; <$db> }; # slurp
close $db;
@packages = @{decode_json($json)};
}
sub usage {
print <<'END';
Usage: pacnet <regexp>
Flags: -h (--help) => show usage information
-s (--sync) => synchronise package database
--nocolor => turn off colouring
END
}
sub MAIN {
my $res = GetOptions(
"s|sync" => sub { getdb },
"h|help" => sub { usage },
"nocolor" => sub { $ENV{'ANSI_COLORS_DISABLED'} = 1 },
);
readdb;
if (scalar @ARGV) {
my $count = 0;
printpackage($_) && $count++
for grep { packagestring($_) =~ $ARGV[0] } @packages;
if ($count == 0) {
say "No matches found";
} elsif ($count > 1) {
say "Found $count matches.";
}
}
}
MAIN;
# vim: ft=perl