141 lines
3.8 KiB
Perl
Executable File
141 lines
3.8 KiB
Perl
Executable File
#!/bin/perl
|
|
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)";
|
|
|
|
my %requests;
|
|
my %responses;
|
|
my %callbacks;
|
|
my $last_handle = 0;
|
|
my $active_requests = 0;
|
|
|
|
sub new_curl( $url, $handle, @headers ) {
|
|
my $curl = WWW::Curl::Easy->new;
|
|
$curl->setopt( CURLOPT_URL, $url );
|
|
$curl->pushopt( CURLOPT_HTTPHEADER, [USERAGENT] );
|
|
for my $header (@headers) {
|
|
$curl->pushopt( CURLOPT_HTTPHEADER, [$header] );
|
|
}
|
|
$curl->setopt( CURLOPT_PRIVATE, $handle );
|
|
$curl->setopt( CURLOPT_WRITEDATA, \$responses{$handle} );
|
|
$requests{$handle} = $curl;
|
|
}
|
|
|
|
sub new_callback( $callback, $handle ) {
|
|
$callbacks{$handle} = $callback;
|
|
}
|
|
|
|
sub exec_multicurl() {
|
|
my $curlm = WWW::Curl::Multi->new;
|
|
for my $handle ( keys %requests ) {
|
|
$curlm->add_handle( $requests{$handle} );
|
|
$active_requests++;
|
|
}
|
|
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--;
|
|
$callbacks{$handle}->();
|
|
delete $requests{$handle};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
sub json_decode($handle) {
|
|
my @tmp = split( '\[\{"', $responses{$handle} );
|
|
my $json_string = '[{"' . $tmp[1];
|
|
return decode_json($json_string);
|
|
}
|
|
|
|
sub git_clone($url) {
|
|
print("git clone --recursive -j8 $url");
|
|
|
|
# TODO:
|
|
}
|
|
|
|
sub json_extract_values( $handle, $key ) {
|
|
my @ret;
|
|
my $i = 0;
|
|
my @tmp = JSON::decode_json ( $responses{$handle} );
|
|
while ( my $value = $tmp[0][ ++$i ]{$key} ) {
|
|
push( @ret, $value );
|
|
}
|
|
return @ret;
|
|
}
|
|
|
|
sub get_target_dir() {
|
|
|
|
}
|
|
|
|
sub handle_repo($url) {
|
|
# TODO: Handle blocking
|
|
# TODO: Handle cloning
|
|
# TODO: Handle pulling
|
|
# TODO: Trigger hooks on clone/pull
|
|
# TODO: Handle automatic directory placement
|
|
}
|
|
|
|
sub get_conf() {
|
|
my $configdir;
|
|
if ( $ENV{XDG_CONFIG_HOME} ) {
|
|
$configdir = "$ENV{XDG_CONFIG_HOME}/clonedev";
|
|
}
|
|
else {
|
|
$configdir = "$ENV{HOME}/.config/clonedev";
|
|
}
|
|
open(my $cfg, '<', $configdir . "/config.yml");
|
|
my ($hashref, $arrayref, $string) = YAML::Load(do { local $/; <$cfg> });
|
|
close($cfg);
|
|
return %$hashref;
|
|
}
|
|
|
|
# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
|
|
# ░ ░░░░ ░░░ ░░░ ░░ ░░░ ░
|
|
# ▒ ▒▒ ▒▒ ▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒ ▒▒ ▒
|
|
# ▓ ▓▓ ▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ▓ ▓ ▓
|
|
# █ █ █ ██ █████ █████ ██ █
|
|
# █ ████ ██ ████ ██ ██ ███ █
|
|
# ████████████████████████████████████████
|
|
|
|
my %conf = get_conf();
|
|
|
|
|
|
|
|
# my $gitea_repos = ++$last_handle;
|
|
# new_curl( "https://git.snaile.de/api/v1/orgs/snailed/repos",
|
|
# $gitea_repos,
|
|
# "Authorization: token 0991e5a3028713e369b7758489c774a3108070b7" );
|
|
# new_callback(
|
|
# sub () {
|
|
# my @urls = json_extract_values( $gitea_repos, "ssh_url" );
|
|
# foreach my $url (@urls) {
|
|
# print($url . "\n");
|
|
# }
|
|
# },
|
|
# $gitea_repos
|
|
# );
|
|
#
|
|
# my $github_octocat = ++$last_handle;
|
|
# new_curl( "https://api.github.com/octocat", $last_handle );
|
|
# new_callback(
|
|
# sub () {
|
|
# print( $responses{$github_octocat} );
|
|
# },
|
|
# $github_octocat
|
|
# );
|
|
|
|
# exec_multicurl();
|