1
0
Fork 0

start clonedev rework in perl

This commit is contained in:
Luca Bilke 2024-02-14 10:35:47 +01:00
parent 9ba4e72210
commit b652230bae
5 changed files with 216 additions and 3 deletions

60
.config/clonedev/config.yml Executable file
View file

@ -0,0 +1,60 @@
base_dir: "${XDG_DOCUMENTS_DIR}/dev"
actions:
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"
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"
block_unsorted: false
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

@ -0,0 +1,2 @@
#!/bin/sh
submodule add -f -- https://git.snaile.de/snailed/matrix-inventory inventory

View file

@ -0,0 +1,2 @@
#!/bin/sh
just roles

140
.local/bin/clonedev.pl Executable file
View file

@ -0,0 +1,140 @@
#!/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();

View file

@ -62,7 +62,16 @@ scroll() {
tput sgr0
}
pull() {
# pull() {
#
# }
#
# clone() {
#
# }
# FIX: pulling isn't functional yet
ensure() {
repo=$1
dir=$(get_path "$repo") || {
warn "$repo blocked ✗"
@ -150,9 +159,9 @@ mkdir -p "$BASE" && cd "$BASE" || {
for target in $TARGETS; do
for repo in $(list "$API_URL/$target/$ENDPOINT" | sed 's|.git$||' | xargs); do
pull "$repo"
ensure "$repo"
done
done
for repo in $(echo "$EXTRA_REPOS" | xargs); do
pull "$repo"
ensure "$repo"
done