From fd0aeda19e427624b56ce3f60a80efc0fba05ba3 Mon Sep 17 00:00:00 2001 From: Luca Bilke Date: Tue, 27 Feb 2024 13:39:46 +0100 Subject: [PATCH] clonedev reformatting, header fixes --- .local/bin/clonedev | 75 +++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 43 deletions(-) diff --git a/.local/bin/clonedev b/.local/bin/clonedev index 697274d60..d247e72a2 100755 --- a/.local/bin/clonedev +++ b/.local/bin/clonedev @@ -16,13 +16,12 @@ $Data::Dumper::Indent = 2; # TODO: Async the git clones # TODO: Show hook/clone output in a prettier fashion (like docker buildx) -use constant USERAGENT => - "User-Agent: MarxBot/4.2.0 (A script reading some information about repos)"; +use constant USERAGENT => "User-Agent: MarxBot/4.2.0 (A script reading some information about repos)"; my @handles; my @messages; my %conf; -my $active_repos = 0; +my $active_repos = 0; my $active_requests = 0; sub info($message) { @@ -45,8 +44,7 @@ sub set_curl( $handle, $url, @headers ) { $handles[$handle]{curl}->pushopt( CURLOPT_HTTPHEADER, [$header] ); } $handles[$handle]{curl}->setopt( CURLOPT_PRIVATE, $handle ); - $handles[$handle]{curl} - ->setopt( CURLOPT_WRITEDATA, \$handles[$handle]{memory} ); + $handles[$handle]{curl}->setopt( CURLOPT_WRITEDATA, \$handles[$handle]{memory} ); } sub add_callback( $handle, $callback ) { @@ -95,11 +93,8 @@ sub url_filter($handle) { for my $repo ( @{ $handles[$handle]{memory} } ) { if ( $repo->{ $lookup->{url_field} } ) { push( @tmp, $repo->{ $lookup->{url_field} } ); - } - else { - error( -"Failed to extract $lookup->{url_field} while processing lookup: $lookup->{name}" - ); + } else { + error( "Failed to extract $lookup->{url_field} while processing lookup: $lookup->{name}" ); } } $handles[$handle]{memory} = \@tmp; @@ -115,10 +110,7 @@ sub process_urls($handle) { my @tmp; for my $url ( @{ $handles[$handle]{memory} } ) { my %repo; - if ( $url =~ -/^((.*(?:@|:\/\/))[a-zA-Z0-9-_.]+(?:\/|:)(?:[a-zA-Z0-9-_.\/]+\/)?([a-zA-Z0-9-_.]+?)\/([a-zA-Z0-9-_.]+?)(?:\.git)?\/?)$/ - ) - { + if ( $url =~ /^((.*(?:@|:\/\/))[a-zA-Z0-9-_.]+(?:\/|:)(?:[a-zA-Z0-9-_.\/]+\/)?([a-zA-Z0-9-_.]+?)\/([a-zA-Z0-9-_.]+?)(?:\.git)?\/?)$/ ) { $repo{url} = $1; $repo{owner} = $3; $repo{name} = $4; @@ -126,12 +118,10 @@ sub process_urls($handle) { if ( substr( $2, -1 ) eq "@" ) { $repo{protocol} = "ssh"; - } - else { + } else { $repo{protocol} = $2; } - } - else { + } else { error("Failed to parse url: $url"); next; } @@ -148,14 +138,15 @@ sub process_urls($handle) { } } - if (!$repo{path} && $conf{lookups}[ $handles[$handle]{lookup} ]{block_unsorted}) { + if ( !$repo{path} && !$conf{lookups}[ $handles[$handle]{lookup} ]{block_unsorted} ) { $repo{path} = `printf $conf{unsorted_directory}/$repo{name}`; + } else { + next; } my $clone_hook = "$conf{hook_dir}/clone/$repo{owner}:$repo{name}"; my $pull_hook = "$conf{hook_dir}/pull/$repo{owner}:$repo{name}"; - ( -x $clone_hook ) - and $repo{clone_hook} = "cd $repo{path} && $clone_hook"; + ( -x $clone_hook ) and $repo{clone_hook} = "cd $repo{path} && $clone_hook"; ( -x $pull_hook ) and $repo{pull_hook} = "cd $repo{path} && $pull_hook"; push( @tmp, \%repo ); } @@ -178,31 +169,26 @@ sub handle_repos($handle) { make_path( $repo->{path} ); info("Cloning $repo->{fullname}"); `git -C '$repo->{path}' clone $conf{clone_flags} '$repo->{url}' .`; - ( $? != 0 ) - and error("Failed to clone $repo->{url} to $repo->{path}"); + ( $? != 0 ) and error("Failed to clone $repo->{url} to $repo->{path}"); if ( $repo->{clone_hook} ) { info("Running clone hook for $repo->{fullname}"); `$repo->{clone_hook}`; } - ( $? != 0 ) - and error("Failed to execute clone hook for $repo->{fullname}"); + ( $? != 0 ) and error("Failed to execute clone hook for $repo->{fullname}"); } elsif ( !folder_is_empty("$repo->{path}/.git") ) { info("Pulling $repo->{fullname} to $repo->{path}"); if (`git -C $repo->{path} status -z`) { warn("$repo->{path} has an unclean tree."); - } - else { + } else { `git -C $repo->{path} pull $conf{pull_flags}`; } - ( $? != 0 ) - and error("Failed to pull $repo->{url} to $repo->{path}"); + ( $? != 0 ) and error("Failed to pull $repo->{url} to $repo->{path}"); if ( $repo->{pull_hook} ) { info("Running pull hook for $repo->{fullname}"); `$repo->{pull_hook}`; } - ( $? != 0 ) - and error("Failed to execute pull hook for $repo->{fullname}"); + ( $? != 0 ) and error("Failed to execute pull hook for $repo->{fullname}"); } } } @@ -211,8 +197,7 @@ sub read_conf() { my $configdir; if ( $ENV{XDG_CONFIG_HOME} ) { $configdir = "$ENV{XDG_CONFIG_HOME}/clonedev"; - } - else { + } else { $configdir = "$ENV{HOME}/.config/clonedev"; } open( my $cfg, '<', $configdir . "/config.yml" ) or die; @@ -221,8 +206,8 @@ sub read_conf() { if ( !$hashref->{hook_dir} ) { $hashref->{hook_dir} = "$configdir/hooks"; } - for my $dir (@{$hashref->{directories}}) { - grep(s/\//\\\//, @{$dir->{repos}}); + for my $dir ( @{ $hashref->{directories} } ) { + grep( s/\//\\\//, @{ $dir->{repos} } ); } %conf = %$hashref; } @@ -231,7 +216,6 @@ sub curl_pipeline($handle) { add_callback( $handle, \&json_decode ); add_callback( $handle, \&url_filter ); add_callback( $handle, \&process_urls ); - # add_callback($handle, \&dump); add_callback( $handle, \&handle_repos ); } @@ -250,24 +234,29 @@ my $last_handle = 0; for my $i ( keys @{ $conf{lookups} } ) { my %lookup = %{ $conf{lookups}[$i] }; chomp( $ENV{TOKEN} = $lookup{token_cmd} ? `$lookup{token_cmd}` : "" ); - for ( $lookup{extra_headers} ) { + for ( @{ $lookup{extra_headers} } ) { $_ = `printf "$_"`; } - if ($lookup{targets}) { + if ( $lookup{targets} ) { for my $j ( keys @{ $lookup{targets} } ) { $last_handle++; $handles[$last_handle]{lookup} = $i; - set_curl( $last_handle, + set_curl( + $last_handle, "$lookup{api_url}/$lookup{targets}[$j]/$lookup{endpoint}", - $lookup{extra_headers} ); + @{ $lookup{extra_headers} } + ); curl_pipeline($last_handle); } - } else { + } + else { $last_handle++; $handles[$last_handle]{lookup} = $i; - set_curl( $last_handle, + set_curl( + $last_handle, "$lookup{api_url}/$lookup{endpoint}", - $lookup{extra_headers} ); + @{ $lookup{extra_headers} } + ); curl_pipeline($last_handle); } }