dotfiles/zsh/dot-zshrc.d/10-workspace.sh

60 lines
1.6 KiB
Bash

#!/bin/bash
alias cdw="cd ${WORKSPACE_DIR}"
function _get_projects() {
( cd -- "${WORKSPACE_DIR}" && find . -mindepth 3 -maxdepth 3 -type d | sed 's|^\./||' )
}
function cdp() {
if command -v fzf &>/dev/null; then
cd -- "${WORKSPACE_DIR}/$(_get_projects | fzf --query "$*")"
return
fi
local projects=( "${(@f)$( _get_projects )}" )
if [[ "$#" -gt 0 ]]; then
projects=( "${(@Mb)projects:#*${1}*}" )
fi
select project in "${projects[@]}"; do
cd -- "${WORKSPACE_DIR}/${project}"
break
done
}
function _cdp_comp() {
local projects=( "${(@f)$( _get_projects )}" )
compset -P '*\/'
_describe "${projects[@]}"
}
compdef _cdp_comp cdp
function clone() {
if [[ "$#" -lt 1 ]]; then
echo "[!] Missing URL to clone!" >&2
return 1
fi
local url="$1"
local forge owner repo
if [[ "${url}" =~ ^ssh:\/\/[a-zA-Z0-9_\-]+@([a-zA-Z0-9_.\-]+):[0-9]+/([a-zA-Z0-9_\-]+)/([a-zA-Z0-9_\-]+)(.git)?$ ]]; then
forge="${match[1]:l}"
owner="${match[2]:l}"
repo="${match[3]:l}"
elif [[ "${url}" =~ ^[a-zA-Z0-9_\-]+@([a-zA-Z0-9_.\-]+):([a-zA-Z0-9_\-]+)\/([a-zA-Z0-9_\-]+)(.git)?$ ]]; then
forge="${match[1]:l}"
owner="${match[2]:l}"
repo="${match[3]:l}"
elif [[ "${url}" =~ ^https?:\/\/([a-zA-Z0-9_.\-]+)(:[0-9]+)?\/([a-zA-Z0-9_\-]+)/([a-zA-Z0-9_\-]+)(.git)?$ ]]; then
forge="${match[1]:l}"
owner="${match[3]:l}"
repo="${match[4]:l}"
else
echo "[!] Do not know how to handle ${url}!" >&2
return 1
fi
git clone "${url}" "${WORKSPACE_DIR}/${forge}/${owner}/${repo}"
}