From 22dacc57dbd01d92cfde1c2994b018826a436d36 Mon Sep 17 00:00:00 2001 From: Jacob Bachmann Date: Mon, 22 Jan 2024 15:20:57 +0100 Subject: [PATCH] feat(scripts/git-clone-list): handle pagination --- .../modules/scripts/git-clone-list.nix | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/users/gandalf/modules/scripts/git-clone-list.nix b/users/gandalf/modules/scripts/git-clone-list.nix index 0889603..9f79df9 100644 --- a/users/gandalf/modules/scripts/git-clone-list.nix +++ b/users/gandalf/modules/scripts/git-clone-list.nix @@ -7,6 +7,8 @@ pkgs.writeShellScriptBin "git-clone-list" '' red="\e[31m" green="\e[32m" + XPATH='//*[@id="user-list-repositories"]//a[not(@class)]/@href' + function help_screen { echo echo -e "''${bold}''${underline}Usage:''${reset} ''${bold}$0''${reset} " @@ -33,15 +35,30 @@ pkgs.writeShellScriptBin "git-clone-list" '' USER=$1 LIST=$2 - echo -e "''${green}>>> Fetching list https://github.com/stars/$USER/lists/$LIST ...''${reset}" - REPOS=$(${pkgs.xidel}/bin/xidel https://github.com/stars/$USER/lists/$LIST --xpath '//*[@id="user-list-repositories"]//a[not(@class)]/@href') + NOT_VISITED=("/stars/$USER/lists/$LIST?page=1") + VISITED=() + REPOS=() + + while [[ "''${#NOT_VISITED[@]}" != 0 ]]; do + echo -e "''${green}>>> Fetching https://github.com''${NOT_VISITED[0]} ...''${reset}" + while IFS= read -r line; do + VISITED+=("''${NOT_VISITED[0]}") + unset NOT_VISITED[0] + if [[ "$line" == "/stars/$USER/lists/$LIST"* ]]; then + if [[ ! "''${VISITED[@]}" =~ "''${line}" ]]; then + NOT_VISITED+=("$line") + fi + else + REPOS+=("$line") + fi + done < <(xidel "https://github.com''${NOT_VISITED[0]}" --xpath $XPATH) + done - NUM_REPOS=$(echo $REPOS | wc -w) IT=1 - for repo in $REPOS; do - echo -e "''${green}>>> [$IT/$NUM_REPOS] Cloning: https://github.com$repo.git ...''${reset}" - ${pkgs.git}/bin/git clone "https://github.com$repo.git" "''${repo:1}" + for repo in "''${REPOS[@]}"; do + echo -e "''${green}>>> [$IT/''${#REPOS[@]}] Cloning https://github.com$repo.git ...''${reset}" + git clone "https://github.com$repo.git" "''${repo:1}" IT=$((IT + 1)) done ''