2024-02-14 10:35:47 +01:00
|
|
|
#!/bin/perl
|
2024-02-15 01:08:31 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# TODO: detect existing repos and automatically modify config file accordingly
|
|
|
|
# TODO: function to detect unclean working trees
|
2024-02-21 14:41:57 +01:00
|
|
|
# TODO: Handle cloning
|
|
|
|
# TODO: Handle pulling
|
|
|
|
# TODO: Trigger hooks on clone/pull
|
2024-02-15 01:08:31 +01:00
|
|
|
#
|
|
|
|
|
2024-02-21 14:41:57 +01:00
|
|
|
print("This shit ain't done yet!\n");
|
|
|
|
# exit;
|
2024-02-17 17:57:36 +01:00
|
|
|
|
2024-02-14 10:35:47 +01:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use feature ("signatures");
|
|
|
|
use YAML();
|
|
|
|
use JSON();
|
|
|
|
use WWW::Curl::Easy;
|
|
|
|
use WWW::Curl::Multi;
|
|
|
|
use Data::Dump();
|
|
|
|
use File::Path();
|
|
|
|
|
|
|
|
use constant USERAGENT =>
|
|
|
|
"User-Agent: MarxBot/4.2.0 (A script reading some information about repos)";
|
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
my @handles;
|
|
|
|
my %conf;
|
|
|
|
my $active_repos = 0;
|
2024-02-14 10:35:47 +01:00
|
|
|
my $active_requests = 0;
|
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
sub set_curl( $handle, $url, @headers ) {
|
|
|
|
$handles[$handle]{curl} = WWW::Curl::Easy->new;
|
|
|
|
$handles[$handle]{curl}->setopt( CURLOPT_URL, $url );
|
|
|
|
$handles[$handle]{curl}->pushopt( CURLOPT_HTTPHEADER, [USERAGENT] );
|
2024-02-14 10:35:47 +01:00
|
|
|
for my $header (@headers) {
|
2024-02-15 01:08:31 +01:00
|
|
|
$handles[$handle]{curl}->pushopt( CURLOPT_HTTPHEADER, [$header] );
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
2024-02-15 01:08:31 +01:00
|
|
|
$handles[$handle]{curl}->setopt( CURLOPT_PRIVATE, $handle );
|
|
|
|
$handles[$handle]{curl}
|
|
|
|
->setopt( CURLOPT_WRITEDATA, \$handles[$handle]{memory} );
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
sub add_callback( $handle, $callback ) {
|
|
|
|
push( @{ $handles[$handle]{callbacks} }, $callback );
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub exec_multicurl() {
|
|
|
|
my $curlm = WWW::Curl::Multi->new;
|
2024-02-15 01:08:31 +01:00
|
|
|
for my $handle (@handles) {
|
|
|
|
if ($handle) {
|
|
|
|
$curlm->add_handle( $handle->{curl} );
|
|
|
|
$active_requests++;
|
|
|
|
}
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
|
|
|
while ($active_requests) {
|
|
|
|
my $active_transfers = $curlm->perform;
|
|
|
|
if ( $active_transfers != $active_requests ) {
|
|
|
|
while ( my ( $handle, $return_value ) = $curlm->info_read ) {
|
|
|
|
if ($handle) {
|
|
|
|
$active_requests--;
|
2024-02-15 01:08:31 +01:00
|
|
|
if ( $handles[$handle]{callbacks} ) {
|
|
|
|
for my $callback ( @{ $handles[$handle]->{callbacks} } )
|
|
|
|
{
|
|
|
|
$callback->($handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete $handles[$handle];
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub json_decode($handle) {
|
2024-02-15 01:08:31 +01:00
|
|
|
$handles[$handle]{memory} = JSON::decode_json( $handles[$handle]{memory} );
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
sub filter($handle) {
|
|
|
|
my @tmp;
|
|
|
|
my $plugin = $conf{plugins}[ $handles[$handle]->{plugin} ];
|
|
|
|
foreach my $repo ( @{ $handles[$handle]{memory} } ) {
|
|
|
|
if ( $repo->{ssh_url} ) {
|
|
|
|
push( @tmp, $repo->{ %$plugin{url_type} } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$handles[$handle]{memory} = \@tmp;
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
sub process_urls($handle) {
|
|
|
|
my @tmp;
|
|
|
|
foreach my $url ( @{ $handles[$handle]{memory} } ) {
|
|
|
|
my %repo;
|
|
|
|
if ( $url =~
|
|
|
|
/^(.*[\/:](([a-zA-Z0-9-_.]+)\/([a-zA-Z0-9-_.]+?))(?:\.git)?\/?)$/ )
|
|
|
|
{
|
|
|
|
$repo{url} = $1;
|
|
|
|
$repo{name} = $2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue if grep /^$repo{name}$/, $conf{block_repos};
|
|
|
|
my $path;
|
|
|
|
foreach my $directory ( @{ $conf{directories} } ) {
|
|
|
|
foreach my $reponame ( @{ %$directory{repos} } ) {
|
|
|
|
if ( $reponame =~ $repo{name} ) {
|
|
|
|
$path = %$directory{path};
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !$path and !$conf{block_unsorted} ) {
|
|
|
|
$path = "\${XDG_DOCUMENTS_DIR}/unsorted_repos";
|
|
|
|
$repo{path} = `printf $path`;
|
|
|
|
push( @tmp, %repo );
|
|
|
|
}
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
2024-02-15 01:08:31 +01:00
|
|
|
$handles[$handle]{memory} = \@tmp;
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
sub dump($handle) {
|
|
|
|
print("------ Handle $handle ------\n");
|
|
|
|
Data::Dump::dump( $handles[$handle]{memory} );
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
# sub handle_repos($handle) {
|
|
|
|
# foreach my $repo (@{$handles[$handle]->{memory}}) {
|
|
|
|
# if (-d $repo{})
|
|
|
|
# }
|
|
|
|
# }
|
2024-02-14 10:35:47 +01:00
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
sub read_conf() {
|
2024-02-14 10:35:47 +01:00
|
|
|
my $configdir;
|
|
|
|
if ( $ENV{XDG_CONFIG_HOME} ) {
|
|
|
|
$configdir = "$ENV{XDG_CONFIG_HOME}/clonedev";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$configdir = "$ENV{HOME}/.config/clonedev";
|
|
|
|
}
|
2024-02-15 01:08:31 +01:00
|
|
|
open( my $cfg, '<', $configdir . "/config.yml" ) or die;
|
|
|
|
my ( $hashref, $arrayref, $string ) = YAML::Load( do { local $/; <$cfg> } );
|
2024-02-14 10:35:47 +01:00
|
|
|
close($cfg);
|
2024-02-15 01:08:31 +01:00
|
|
|
%conf = %$hashref;
|
2024-02-14 10:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
|
|
|
|
# ░ ░░░░ ░░░ ░░░ ░░ ░░░ ░
|
|
|
|
# ▒ ▒▒ ▒▒ ▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒ ▒▒ ▒
|
|
|
|
# ▓ ▓▓ ▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ▓ ▓ ▓
|
|
|
|
# █ █ █ ██ █████ █████ ██ █
|
|
|
|
# █ ████ ██ ████ ██ ██ ███ █
|
|
|
|
# ████████████████████████████████████████
|
|
|
|
|
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
read_conf();
|
2024-02-14 10:35:47 +01:00
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
# $last_handle++;
|
|
|
|
# set_curl( $last_handle, "https://api.github.com/octocat" );
|
|
|
|
# add_callback(
|
|
|
|
# $last_handle,
|
|
|
|
# sub ($handle) {
|
|
|
|
# print( $handles[$handle]{memory} );
|
|
|
|
# }
|
2024-02-14 10:35:47 +01:00
|
|
|
# );
|
|
|
|
#
|
2024-02-15 01:08:31 +01:00
|
|
|
my $last_handle = 0;
|
2024-02-18 13:02:37 +01:00
|
|
|
foreach my $i ( keys @{ $conf{plugins} } ) {
|
|
|
|
my %plugin = %{ $conf{plugins}[$i] };
|
|
|
|
chomp(
|
|
|
|
$ENV{TOKEN} =
|
2024-02-15 01:08:31 +01:00
|
|
|
$plugin{token_cmd}
|
|
|
|
? `$plugin{token_cmd}`
|
2024-02-18 13:02:37 +01:00
|
|
|
: ""
|
|
|
|
);
|
2024-02-15 01:08:31 +01:00
|
|
|
foreach ( @{ $plugin{extra_headers} } ) {
|
|
|
|
last if ( !$_ );
|
|
|
|
$_ = `printf "$_"`;
|
|
|
|
}
|
|
|
|
foreach my $j ( keys @{ $plugin{targets} } ) {
|
|
|
|
$last_handle++;
|
|
|
|
$handles[$last_handle]{plugin} = $i;
|
2024-02-21 14:41:57 +01:00
|
|
|
print("$plugin{api_url}/$plugin{targets}[$j]/$plugin{endpoint}\n");
|
|
|
|
set_curl( $last_handle, "$plugin{api_url}/$plugin{targets}[$j]/$plugin{endpoint}",
|
2024-02-18 13:02:37 +01:00
|
|
|
$plugin{extra_headers} );
|
2024-02-15 01:08:31 +01:00
|
|
|
add_callback( $last_handle, \&dump );
|
|
|
|
add_callback( $last_handle, \&json_decode );
|
|
|
|
add_callback( $last_handle, \&filter );
|
|
|
|
add_callback( $last_handle, \&process_urls );
|
|
|
|
add_callback( $last_handle, \&dump );
|
|
|
|
}
|
|
|
|
}
|
2024-02-14 10:35:47 +01:00
|
|
|
|
2024-02-15 01:08:31 +01:00
|
|
|
exec_multicurl();
|