#!/usr/bin/env perl
our
$VERSION
=
'1.000000'
;
sub
execute;
my
$bin
=
shift
@ARGV
;
if
(not
$bin
) {
print
"🤗 No binary provided\n"
;
exit
0;
}
my
$local_lib
= getcwd .
"/.cpx"
;
if
(-f
"$local_lib/bin/$bin"
) {
print
"âš“ Found executable already installed\n"
;
execute(
$local_lib
,
$bin
);
}
my
$es
= Search::Elasticsearch->new(
client
=>
'2_0::Direct'
,
cxn_pool
=>
'Static::NoPing'
,
send_get_body_as
=>
'POST'
,
);
my
$files
=
undef
;
my
@hits
= ();
foreach
my
$dir
(
"bin"
,
"scripts"
,
"script"
,
"."
) {
my
$path
=
"$bin"
;
if
(
$dir
ne
"."
) {
$path
=
"$dir/"
.
$path
}
$files
=
$es
->search(
index
=>
'cpan'
,
type
=>
'file'
,
size
=> 10,
body
=> {
query
=> {
filtered
=> {
query
=> {
match_all
=> {} },
filter
=> {
and
=> [
{
term
=> {
'path'
=>
"$path"
} },
{
term
=> {
'directory'
=> \0 } },
{
term
=> {
'status'
=>
'latest'
} },
]
},
},
},
},
fields
=> [
'release'
,
'author'
,
'download_url'
],
);
@hits
= @{
$files
->{hits}->{hits} };
if
(
scalar
@hits
> 0) {
print
"🎯 Found [$dir/$bin]\n"
;
last
;
}
}
if
(
scalar
@hits
< 1) {
print
"😞 Not found\n"
;
exit
-1;
}
my
$to_install
=
$hits
[0]->{fields}->{download_url};
print
"📦 Release to install [$to_install]\n"
;
print
"🔧 Will install into $local_lib\n"
;
my
$cpm
= App::cpm::CLI->new;
$cpm
->run(
'install'
,
"-L$local_lib"
,
$to_install
);
sub
execute {
my
$ll
=
shift
;
my
$b
=
shift
;
if
(-f
"$ll/bin/$b"
) {
exec
"perl"
,
"-Mlocal::lib=$ll"
,
"$ll/bin/$b"
,
@ARGV
;
}
else
{
print
"💀 Executable was found, but not installed correctly\n"
;
exit
-1;
}
}
execute(
$local_lib
,
$bin
);