From 99714482dea6b3bb65d05d8f97ba627fd8b3f269 Mon Sep 17 00:00:00 2001 From: sadan <117494111+sadan4@users.noreply.github.com> Date: Mon, 12 May 2025 19:18:53 -0400 Subject: [PATCH] add clone repo commannd --- .../homeModules/scripts/cloneRepoImpl.sh | 84 +++++++++++++++++++ common/users/homeModules/scripts/default.nix | 43 +++++++++- 2 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 common/users/homeModules/scripts/cloneRepoImpl.sh diff --git a/common/users/homeModules/scripts/cloneRepoImpl.sh b/common/users/homeModules/scripts/cloneRepoImpl.sh new file mode 100644 index 0000000..f0bbb84 --- /dev/null +++ b/common/users/homeModules/scripts/cloneRepoImpl.sh @@ -0,0 +1,84 @@ +set -e +echoe() { + echo $* 1>&2 +} + +getTotalRepoCount() { + echo $(gh api user -q ".total_private_repos + .public_repos") +} + +showGuiAndPickRepo() { + local totalRepos + totalRepos=$(getTotalRepoCount) + if [[ $? -ne 0 ]]; then return 1; fi + local pick + pick=$(gh repo list -L ${totalRepos} --json nameWithOwner -q ".[] | .nameWithOwner" | fzf \ + --scheme path \ + --cycle \ + --filepath-word \ + --height 50% \ + --reverse \ + --border \ + --border-label " Select Repo To Clone " \ + --color label:bold:cyan \ + --tiebreak index + ) + if [[ $? -ne 0 ]]; then return 1; fi + echo ${pick} +} + +permuteNames() { + local name=$1 + IFS="/" read -r user path <<< $name + local options + options=("${path}" "${user}-${path}") + if [[ $? -ne 0 ]]; then return 1; fi + echo ${options} +} + +askCloneName() { + local repo=$1 + read -ra possibleNames <<< $(permuteNames ${repo}) + local allNames + allNames=${possibleNames[@]} + if [[ $? -ne 0 ]]; then return 1; fi + local pick + pick=$(echo ${allNames// /\\n} | fzf \ + --query "${possibleNames[1]}" \ + --print-query \ + --height "~50%" \ + --cycle \ + --border \ + --border-label " Name of Dir to Clone Into " \ + --color label:bold:cyan + ) + # with --print-query we still get a negative exit code if the response was not an option + if [[ -z $pick ]]; then return 1; fi + + if echo $pick | grep -q "/"; then + echoe "Your selection: $(echo $pick | grep --color=always "/") has a path delimiter (/) and is invalid"; + return 1; + fi + + echo ${pick} +} + +repo=$(showGuiAndPickRepo) +if [[ $? -ne 0 ]]; then exit 1; fi +USE_SSH=true +if [[ $? -ne 0 ]]; then exit 1; fi +url="ERROR_INVALID_URL" +if [[ $? -ne 0 ]]; then exit 1; fi + +if $USE_SSH; then + url="git@github.com:${repo}.git" +else + url="https://github.com/${repo}.git" +fi + +cloneName=$(askCloneName ${repo}) +if [[ $? -ne 0 ]]; then exit 1; fi + +gh repo clone ${url} ${cloneName} + +echo ${cloneName} diff --git a/common/users/homeModules/scripts/default.nix b/common/users/homeModules/scripts/default.nix index ffb5888..d2c5540 100644 --- a/common/users/homeModules/scripts/default.nix +++ b/common/users/homeModules/scripts/default.nix @@ -45,12 +45,16 @@ in (mkScript { name = "http2ssh"; file = ./http2ssh.sh; - env = [ pkgs.git ]; + env = with pkgs; [ + git + ]; }) (mkScript { name = "git_fetchAll"; file = ./git_fetchAll.sh; - env = [ pkgs.git ]; + env = with pkgs; [ + git + ]; }) (mkScript { name = "install_eslint"; @@ -59,7 +63,9 @@ in (mkScript { name = "math"; file = ./math.sh; - env = [ pkgs.python3 ]; + env = with pkgs; [ + python3 + ]; }) (mkScript { name = "hashi18n"; @@ -68,8 +74,11 @@ in (mkScript { name = "flakeify"; file = ./flakeify.sh; - env = [ pkgs.direnv ]; + env = with pkgs; [ + direnv + ]; }) + # impl for the cloneRepo command ]; file = { scripts = { @@ -87,4 +96,30 @@ in p = "${builtins.readFile ./projectPicker.sh}"; }; }; + programs = { + zsh = { + initExtra = + let + implName = "cloneRepoImpl"; + in + '' + cloneRepo() { + local dir; + dir=$(${ + (mkScript { + name = implName; + file = ./cloneRepoImpl.sh; + env = with pkgs; [ + gh + fzf + ]; + }) + }/bin/${implName}) + if [[ $? -eq 0 ]]; then + cd ''${dir} + fi + } + ''; + }; + }; }