1
0
Fork 0

more work on clonedev script

This commit is contained in:
Luca Bilke 2024-02-15 01:08:31 +01:00
parent b652230bae
commit aa9d3dd1be
2 changed files with 170 additions and 114 deletions

View file

@ -1,60 +1,62 @@
base_dir: "${XDG_DOCUMENTS_DIR}/dev"
actions:
gitea_owned_repos:
plugins:
- name: "gitea_owned_repos"
api_url: "https://git.snaile.de/api/v1"
targets:
- "orgs/snailed"
- "users/luca"
endpoint: "repos"
token_cmd: "secret-tool lookup gitea api_token"
# read:organization
# read:repository
# read:user
extra_headers:
- "Authorization: token ${TOKEN}"
json_filter: ".[].ssh_url"
github_starred_repos:
api_url: "https://api.github.com"
targets: "users/ssnailed"
endpoint: "starred"
json_filter: ".[].ssh_url"
url_type: "ssh_url"
# - name: "github_starred_repos"
# api_url: "https://api.github.com"
# targets:
# - "users/ssnailed"
# endpoint: "starred"
# url_type: "ssh_url"
# token_cmd: "secret-tool lookup github api_token"
# extra_headers:
# - "Authorization: Bearer ${TOKEN}"
repos:
- "https://git.kernel.org/pub/scm/git/git.git"
clone_flags: "--recursive -j8"
subdirectories:
desktop:
- "luca/bootstrapper"
- "luca/dmenu-custom"
- "luca/dwm-custom"
- "luca/dwmblocks-custom"
- "luca/st-custom"
- "luca/tokyonight-icons"
- "luca/tokyonight-theme"
server:
- "snailed/ansible-example"
- "snailed/ansible-homelab"
- "snailed/dotfiles-server"
- "snailed/qbittorrent-natpmp"
- "snailed/server-resources"
- "snailed/void-packages-custom"
- "snailed/xbps-builder"
- "spantaleev/matrix-docker-ansible-deploy"
- "void-linux/void-packages"
nvim:
- "snailed/taolf"
misc:
- "snailed/tidal-scraper"
- "b3nj5m1n/xdg-ninja"
- "jnweiger/led-name-badge-ls32"
directories:
- path: "${XDG_DOCUMENTS_DIR}/dev/desktop"
repos:
- "luca/bootstrapper"
- "luca/dmenu-custom"
- "luca/dwm-custom"
- "luca/dwmblocks-custom"
- "luca/st-custom"
- "luca/tokyonight-icons"
- "luca/tokyonight-theme"
- path: "${XDG_DOCUMENTS_DIR}/dev/server"
repos:
- "snailed/ansible-example"
- "snailed/ansible-homelab"
- "snailed/dotfiles-server"
- "snailed/qbittorrent-natpmp"
- "snailed/server-resources"
- "snailed/void-packages-custom"
- "snailed/xbps-builder"
- "spantaleev/matrix-docker-ansible-deploy"
- "void-linux/void-packages"
- path: "${XDG_DOCUMENTS_DIR}/dev/nvim"
repos:
- "snailed/taolf"
- path: "${XDG_DOCUMENTS_DIR}/dev/misc"
repos:
- "snailed/tidal-scraper"
- "b3nj5m1n/xdg-ninja"
- "jnweiger/led-name-badge-ls32"
block_unsorted: false
block_unsorted: 0
unsorted_directory: "${XDG_DOCUMENTS_DIR}/dev/unsorted"
block_repos:
- "luca/dotfiles"
- "luca/pinentry-dmenu-custom"
- "snailed/matrix-inventory"
extra_repos:
- "https://github.com/void-linux/void-packages"
- "https://github.com/spantaleev/matrix-docker-ansible-deploy"

View file

@ -1,4 +1,11 @@
#!/bin/perl
#
# TODO: detect existing repos and automatically modify config file accordingly
# TODO: function to detect unclean working trees
# TODO: Move update hooks to the actual .git/hooks directory
#
use strict;
use warnings;
use feature ("signatures");
@ -12,33 +19,34 @@ 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 @handles;
my %conf;
my $active_repos = 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] );
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] );
for my $header (@headers) {
$curl->pushopt( CURLOPT_HTTPHEADER, [$header] );
$handles[$handle]{curl}->pushopt( CURLOPT_HTTPHEADER, [$header] );
}
$curl->setopt( CURLOPT_PRIVATE, $handle );
$curl->setopt( CURLOPT_WRITEDATA, \$responses{$handle} );
$requests{$handle} = $curl;
$handles[$handle]{curl}->setopt( CURLOPT_PRIVATE, $handle );
$handles[$handle]{curl}
->setopt( CURLOPT_WRITEDATA, \$handles[$handle]{memory} );
}
sub new_callback( $callback, $handle ) {
$callbacks{$handle} = $callback;
sub add_callback( $handle, $callback ) {
push( @{ $handles[$handle]{callbacks} }, $callback );
}
sub exec_multicurl() {
my $curlm = WWW::Curl::Multi->new;
for my $handle ( keys %requests ) {
$curlm->add_handle( $requests{$handle} );
$active_requests++;
for my $handle (@handles) {
if ($handle) {
$curlm->add_handle( $handle->{curl} );
$active_requests++;
}
}
while ($active_requests) {
my $active_transfers = $curlm->perform;
@ -46,8 +54,13 @@ sub exec_multicurl() {
while ( my ( $handle, $return_value ) = $curlm->info_read ) {
if ($handle) {
$active_requests--;
$callbacks{$handle}->();
delete $requests{$handle};
if ( $handles[$handle]{callbacks} ) {
for my $callback ( @{ $handles[$handle]->{callbacks} } )
{
$callback->($handle);
}
}
delete $handles[$handle];
}
}
}
@ -55,40 +68,64 @@ sub exec_multicurl() {
}
sub json_decode($handle) {
my @tmp = split( '\[\{"', $responses{$handle} );
my $json_string = '[{"' . $tmp[1];
return decode_json($json_string);
$handles[$handle]{memory} = JSON::decode_json( $handles[$handle]{memory} );
}
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 );
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} } );
}
}
return @ret;
$handles[$handle]{memory} = \@tmp;
}
sub get_target_dir() {
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 );
}
}
$handles[$handle]{memory} = \@tmp;
}
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 dump($handle) {
print("------ Handle $handle ------\n");
Data::Dump::dump( $handles[$handle]{memory} );
}
sub get_conf() {
# sub handle_repos($handle) {
# foreach my $repo (@{$handles[$handle]->{memory}}) {
# if (-d $repo{})
# }
# }
sub read_conf() {
my $configdir;
if ( $ENV{XDG_CONFIG_HOME} ) {
$configdir = "$ENV{XDG_CONFIG_HOME}/clonedev";
@ -96,10 +133,10 @@ sub get_conf() {
else {
$configdir = "$ENV{HOME}/.config/clonedev";
}
open(my $cfg, '<', $configdir . "/config.yml");
my ($hashref, $arrayref, $string) = YAML::Load(do { local $/; <$cfg> });
open( my $cfg, '<', $configdir . "/config.yml" ) or die;
my ( $hashref, $arrayref, $string ) = YAML::Load( do { local $/; <$cfg> } );
close($cfg);
return %$hashref;
%conf = %$hashref;
}
# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
@ -110,31 +147,48 @@ sub get_conf() {
# █ ████ ██ ████ ██ ██ ███ █
# ████████████████████████████████████████
my %conf = get_conf();
# TODO: Handle cloning
# TODO: Handle pulling
# TODO: Trigger hooks on clone/pull
read_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
# $last_handle++;
# set_curl( $last_handle, "https://api.github.com/octocat" );
# add_callback(
# $last_handle,
# sub ($handle) {
# print( $handles[$handle]{memory} );
# }
# );
#
# my $github_octocat = ++$last_handle;
# new_curl( "https://api.github.com/octocat", $last_handle );
# new_callback(
# sub () {
# print( $responses{$github_octocat} );
# },
# $github_octocat
# );
my $last_handle = 0;
foreach my $i ( keys @{$conf{plugins}} ) {
my %plugin = %{$conf{plugins}[$i]};
chomp( $ENV{TOKEN} =
$plugin{token_cmd}
? `$plugin{token_cmd}`
: "" );
foreach ( @{ $plugin{extra_headers} } ) {
last if ( !$_ );
$_ = `printf "$_"`;
}
foreach my $j ( keys @{ $plugin{targets} } ) {
# FIX: $_[$j] is empty
$last_handle++;
$handles[$last_handle]{plugin} = $i;
print("$plugin{api_url}/$_[$j]/$plugin{endpoint}\n");
set_curl(
$last_handle,
"$plugin{api_url}/$_[$j]/$plugin{endpoint}",
$plugin{extra_headers}
);
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 );
}
}
# exec_multicurl();
exec_multicurl();