From b44a4bf7ef38a92c39c10993094fad7a8a6aed34 Mon Sep 17 00:00:00 2001
From: Luca Bilke <luca@bil.ke>
Date: Tue, 27 Feb 2024 01:49:55 +0100
Subject: [PATCH] more work on clonedev

---
 .config/clonedev/config.yml | 26 ++++++++++++--------------
 .local/bin/clonedev         | 31 +++++++++++++++++++++++--------
 2 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/.config/clonedev/config.yml b/.config/clonedev/config.yml
index 573b4746..7391601b 100755
--- a/.config/clonedev/config.yml
+++ b/.config/clonedev/config.yml
@@ -11,24 +11,22 @@ lookups:
       - "users/luca"
     extra_headers:
       - "Authorization: token ${TOKEN}"
-  # - name: "github_starred_repos"
-  #   api_url: "https://api.github.com"
-  #   endpoint: "starred"
-  #   url_field: "ssh_url"
-  #   token_cmd: "rbw get 'Github' -f 'Clonedev PAT'"
-  #   block_unsorted: true
-  #   targets: 
-  #     - "users/ssnailed"
-  #   extra_headers:
-  #     - "Authorization: Bearer ${TOKEN}"
+  - name: "github_starred_repos"
+    api_url: "https://api.github.com"
+    endpoint: "starred"
+    url_field: "ssh_url"
+    token_cmd: "rbw get 'Github' -f 'Clonedev PAT'"
+    block_unsorted: true
+    targets: 
+      - "users/ssnailed"
+    extra_headers:
+      - "Authorization: Bearer ${TOKEN}"
   # - name: "tralios_gitlab"
-  #   api_url: "https://gitlab.tralios.de/api/v4/"
-  #   endpoint:
+  #   api_url: "https://gitlab.tralios.de/api/v4"
+  #   endpoint: "projects"
   #   url_field: "ssh_url_to_repo"
   #   token_cmd: "rbw get 'Gitlab Tralios' -f 'Clonedev PAT'"
   #   block_unsorted: true
-  #   targets: 
-  #     -
   #   extra_headers:
   #     - "Authorization: Bearer ${TOKEN}"
 
diff --git a/.local/bin/clonedev b/.local/bin/clonedev
index 4d806c85..7a814a11 100755
--- a/.local/bin/clonedev
+++ b/.local/bin/clonedev
@@ -72,10 +72,12 @@ sub exec_multicurl() {
     while ($active_requests) {
         my $active_transfers = $curlm->perform;
         if ( $active_transfers != $active_requests ) {
-            while ( my ( $handle, $return_value ) = $curlm->info_read ) {
+            while ( my ( $handle, $ret ) = $curlm->info_read ) {
                 if ($handle) {
                     $active_requests--;
                     exec_callbacks($handle);
+                    # TODO: proper error checking
+                    # $handles[$handle]{curl}->getinfo(CURLINFO_HTTP_CODE);
                     delete $handles[$handle];
                 }
             }
@@ -225,6 +227,14 @@ sub read_conf() {
     %conf = %$hashref;
 }
 
+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 );
+}
+
 # ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
 # ░  ░░░░  ░░░      ░░░        ░░   ░░░  ░
 # ▒   ▒▒   ▒▒  ▒▒▒▒  ▒▒▒▒▒  ▒▒▒▒▒    ▒▒  ▒
@@ -243,17 +253,22 @@ for my $i ( keys @{ $conf{lookups} } ) {
     for ( $lookup{extra_headers} ) {
         $_ = `printf "$_"`;
     }
-    for my $j ( keys @{ $lookup{targets} } ) {
+    if ($lookup{targets}) {
+        for my $j ( keys @{ $lookup{targets} } ) {
+            $last_handle++;
+            $handles[$last_handle]{lookup} = $i;
+            set_curl( $last_handle,
+                "$lookup{api_url}/$lookup{targets}[$j]/$lookup{endpoint}",
+                $lookup{extra_headers} );
+            curl_pipeline($last_handle);
+        }
+    } else {
         $last_handle++;
         $handles[$last_handle]{lookup} = $i;
         set_curl( $last_handle,
-            "$lookup{api_url}/$lookup{targets}[$j]/$lookup{endpoint}",
+            "$lookup{api_url}/$lookup{endpoint}",
             $lookup{extra_headers} );
-        add_callback( $last_handle, \&json_decode );
-        add_callback( $last_handle, \&url_filter );
-        add_callback( $last_handle, \&process_urls );
-        add_callback( $last_handle, \&dump );
-        # add_callback( $last_handle, \&handle_repos );
+        curl_pipeline($last_handle);
     }
 }