From d39af3d18187ba2b3842bc36e9e82783d27bbe97 Mon Sep 17 00:00:00 2001
From: Luca Bilke <luca@bil.ke>
Date: Tue, 27 Feb 2024 01:21:24 +0100
Subject: [PATCH] allow regex in path definition for clonedev

---
 .config/clonedev/config.yml | 61 ++++++++++++++++++++++---------------
 .local/bin/clonedev         | 50 ++++++++++++------------------
 2 files changed, 55 insertions(+), 56 deletions(-)

diff --git a/.config/clonedev/config.yml b/.config/clonedev/config.yml
index a9f38a49..573b4746 100755
--- a/.config/clonedev/config.yml
+++ b/.config/clonedev/config.yml
@@ -1,25 +1,36 @@
+    # token_cmd: "secret-tool lookup gitea api_token"
 lookups:
   - name: "gitea_owned_repos"
     api_url: "https://git.snaile.de/api/v1"
     endpoint: "repos"
-    url_type: "ssh_url"
-    token_cmd: "secret-tool lookup gitea api_token"
-    block_unsorted: 0
+    url_field: "ssh_url"
+    token_cmd: "rbw get 'Gitea Snailed' -f 'Clonedev PAT'"
+    block_unsorted: jalse
     targets: 
       - "orgs/snailed"
       - "users/luca"
     extra_headers:
       - "Authorization: token ${TOKEN}"
-  - name: "github_starred_repos"
-    api_url: "https://api.github.com"
-    endpoint: "starred"
-    url_type: "ssh_url"
-    token_cmd: "secret-tool lookup github api_token"
-    block_unsorted: 1
-    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:
+  #   url_field: "ssh_url_to_repo"
+  #   token_cmd: "rbw get 'Gitlab Tralios' -f 'Clonedev PAT'"
+  #   block_unsorted: true
+  #   targets: 
+  #     -
+  #   extra_headers:
+  #     - "Authorization: Bearer ${TOKEN}"
 
 extra_urls:
   # - "https://git.kernel.org/pub/scm/git/git.git"
@@ -37,17 +48,6 @@ directories:
       - "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-docker"
-      - "snailed/papermc-docker"
-      - "snailed/server-resources"
-      - "snailed/void-packages-custom"
-      - "snailed/xbps-builder"
-      - "spantaleev/matrix-docker-ansible-deploy"
   - path: "${XDG_DOCUMENTS_DIR}/dev/nvim"
     repos:
       - "snailed/taolf"
@@ -59,10 +59,21 @@ directories:
   - path: "${XDG_DOCUMENTS_DIR}/dev"
     repos:
       - "void-linux/void-packages"
+  - path: "${XDG_DOCUMENTS_DIR}/dev/server"
+    repos:
+      - "snailed/ansible-example"
+      - "snailed/ansible-homelab"
+      - "snailed/qbittorrent-natpmp-docker"
+      - "snailed/papermc-docker"
+      - "snailed/server-resources"
+      - "snailed/void-packages-custom"
+      - "snailed/xbps-builder"
+      - "luca/dotfiles-server"
+      - "spantaleev/matrix-docker-ansible-deploy"
 
 unsorted_directory: "${XDG_DOCUMENTS_DIR}/dev/unsorted"
 
-block_repos: 
+skip_repos: 
   - "luca/dotfiles"
   - "luca/pinentry-dmenu-custom"
   - "snailed/matrix-inventory"
diff --git a/.local/bin/clonedev b/.local/bin/clonedev
index c0257f32..4d806c85 100755
--- a/.local/bin/clonedev
+++ b/.local/bin/clonedev
@@ -134,37 +134,21 @@ sub process_urls($handle) {
             next;
         }
 
-        next if (grep({ $_ eq $repo{fullname} } @{$conf{skip_repos}}));
+        next if ( grep( $_ eq $repo{fullname}, @{ $conf{skip_repos} } ) );
 
         my $path;
-        for my $directory ( @{ $conf{directories} } ) {
-            if (grep({ $_ eq $repo{fullname} } @{ %$directory{repos} })) {
-                $path = %$directory{path};
-                last;
+        DIRS: for my $directory ( @{ $conf{directories} } ) {
+            for my $regex ( @{ $directory->{repos} } ) {
+                if ( $repo{fullname} =~ /$regex/ ) {
+                    $repo{path} = `printf $directory->{path}/$repo{name}`;
+                    last DIRS;
+                }
             }
         }
 
-        if ( !$handles[$handle]{lookup} ) {
-            if ($path) {
-                $repo{path} = `printf $path/$repo{name}`;
-            }
-            else {
-                $repo{path} = `printf $conf{unsorted_directory}/$repo{name}`;
-            }
-
-        }
-        elsif ( !$conf{lookups}[ $handles[$handle]{lookup} ]{block_unsorted}
-            && $repo{path} )
-        {
+        if (!$repo{path} && $conf{lookups}[ $handles[$handle]{lookup} ]{block_unsorted}) {
             $repo{path} = `printf $conf{unsorted_directory}/$repo{name}`;
         }
-        elsif ($path) {
-            $repo{path} = `printf $path/$repo{name}`;
-        }
-        else {
-            warning("Skipping $repo{fullname}");
-            next;
-        }
 
         my $clone_hook = "$conf{hook_dir}/clone/$repo{owner}:$repo{name}";
         my $pull_hook  = "$conf{hook_dir}/pull/$repo{owner}:$repo{name}";
@@ -175,15 +159,15 @@ sub process_urls($handle) {
     }
     $handles[$handle]{memory} = \@tmp;
 }
-#
-# sub dump($handle) {
-#     print("------ Handle $handle ------\n");
-#     print Dumper( $handles[$handle]->{memory} );
-# }
+
+sub dump($handle) {
+    print("------ Handle $handle ------\n");
+    print Dumper( $handles[$handle]->{memory} );
+}
 
 sub folder_is_empty($directory) {
     opendir( my $dh, $directory ) or return 1;
-    return scalar( grep { $_ ne "." && $_ ne ".." } readdir($dh) ) == 0;
+    return scalar( grep( $_ ne "." && $_ ne "..", readdir($dh) ) ) == 0;
 }
 
 sub handle_repos($handle) {
@@ -235,6 +219,9 @@ sub read_conf() {
     if ( !$hashref->{hook_dir} ) {
         $hashref->{hook_dir} = "$configdir/hooks";
     }
+    for my $dir (@{$hashref->{directories}}) {
+        grep(s/\//\\\//, @{$dir->{repos}});
+    }
     %conf = %$hashref;
 }
 
@@ -265,7 +252,8 @@ for my $i ( keys @{ $conf{lookups} } ) {
         add_callback( $last_handle, \&json_decode );
         add_callback( $last_handle, \&url_filter );
         add_callback( $last_handle, \&process_urls );
-        add_callback( $last_handle, \&handle_repos );
+        add_callback( $last_handle, \&dump );
+        # add_callback( $last_handle, \&handle_repos );
     }
 }