From b4f50ad63a92e992a244d6f9e10a083426ae212f Mon Sep 17 00:00:00 2001 From: Jacob Bachmann Date: Mon, 22 Jan 2024 12:24:56 +0100 Subject: [PATCH] feat(scripts): add git-clone-list --- users/gandalf/modules/scripts/default.nix | 1 + .../modules/scripts/git-clone-list.nix | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 users/gandalf/modules/scripts/git-clone-list.nix diff --git a/users/gandalf/modules/scripts/default.nix b/users/gandalf/modules/scripts/default.nix index 3ad0dab..a4d0d46 100644 --- a/users/gandalf/modules/scripts/default.nix +++ b/users/gandalf/modules/scripts/default.nix @@ -1,5 +1,6 @@ { pkgs, nixosConfig, ... }: { home.packages = [ + (import ./git-clone-list.nix { inherit pkgs; }) (import ./nix-update.nix { inherit pkgs nixosConfig; }) ]; } diff --git a/users/gandalf/modules/scripts/git-clone-list.nix b/users/gandalf/modules/scripts/git-clone-list.nix new file mode 100644 index 0000000..0889603 --- /dev/null +++ b/users/gandalf/modules/scripts/git-clone-list.nix @@ -0,0 +1,47 @@ +{ pkgs }: + +pkgs.writeShellScriptBin "git-clone-list" '' + reset="\e[0m" + bold="\e[1m" + underline="\e[4m" + red="\e[31m" + green="\e[32m" + + function help_screen { + echo + echo -e "''${bold}''${underline}Usage:''${reset} ''${bold}$0''${reset} " + echo + echo -e "''${bold}''${underline}Arguments''${reset}:" + echo -e " ''${bold}USER''${reset} Github username of target user" + echo -e " ''${bold}LIST''${reset} List name of target list" + echo + } + + function error { + echo + echo -e "''${red}>>> ''${bold}Error''${reset}''${red}: $1''${reset}" + if [ "$2" == "help" ]; then + help_screen + fi + exit 1 + } + + if [ $# -ne 2 ]; then + error "Expected 2 arguments, got $#" help + fi + + 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') + + 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}" + IT=$((IT + 1)) + done +''