Make cdp and clone more generic. Add a simple toolbx utility. Refactor
zsh config to use new cdp and clone scripts.
This commit is contained in:
parent
f105f890ce
commit
e5c10e3398
9 changed files with 271 additions and 60 deletions
|
|
@ -30,3 +30,15 @@ unset rc
|
||||||
if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ] && [ -z "$SSH_CONNECTION" ]; then
|
if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ] && [ -z "$SSH_CONNECTION" ]; then
|
||||||
exec tmux
|
exec tmux
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Aliases
|
||||||
|
WORKSPACE_DIR="${HOME}/spaces/workspace"
|
||||||
|
export WORKSPACE_DIR
|
||||||
|
|
||||||
|
alias vi="neovim"
|
||||||
|
alias vim="neovim"
|
||||||
|
alias cdw="cd -- ${WORKSPACE_DIR}"
|
||||||
|
|
||||||
|
function cdp() {
|
||||||
|
cd -- "$(_cdp_path "$@")"
|
||||||
|
}
|
||||||
|
|
|
||||||
3
stowers/shell-utils/config.sh
Normal file
3
stowers/shell-utils/config.sh
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
COMMAND_DEPS=("bash")
|
||||||
29
stowers/shell-utils/dot-local/bin/_cdp_path
Executable file
29
stowers/shell-utils/dot-local/bin/_cdp_path
Executable file
|
|
@ -0,0 +1,29 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
function _get_projects() {
|
||||||
|
( cd -- "${WORKSPACE_DIR}" && find . -mindepth 3 -maxdepth 3 -type d | sed 's|^\./||' )
|
||||||
|
}
|
||||||
|
|
||||||
|
function _cdp() {
|
||||||
|
if command -v fzf &>/dev/null; then
|
||||||
|
echo "${WORKSPACE_DIR}/$(_get_projects | fzf --query "$*")"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
readarray -t projects <<< "$(_get_projects)"
|
||||||
|
local filtered_projects
|
||||||
|
filtered_projects=()
|
||||||
|
|
||||||
|
for project in "${projects[@]}"; do
|
||||||
|
if [[ "${project}" == "$1"* ]]; then
|
||||||
|
filtered_projects+=("${project}")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
select project in "${filtered_projects[@]}"; do
|
||||||
|
echo "${WORKSPACE_DIR}/${project}"
|
||||||
|
break
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
_cdp "$@"
|
||||||
31
stowers/shell-utils/dot-local/bin/clone
Executable file
31
stowers/shell-utils/dot-local/bin/clone
Executable file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
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="${BASH_REMATCH[1],,}"
|
||||||
|
owner="${BASH_REMATCH[2],,}"
|
||||||
|
repo="${BASH_REMATCH[3],,}"
|
||||||
|
elif [[ "${url}" =~ ^[a-zA-Z0-9_\-]+@([a-zA-Z0-9_.\-]+):([a-zA-Z0-9_\-]+)\/([a-zA-Z0-9_\-]+)(.git)?$ ]]; then
|
||||||
|
forge="${BASH_REMATCH[1],,}"
|
||||||
|
owner="${BASH_REMATCH[2],,}"
|
||||||
|
repo="${BASH_REMATCH[3],,}"
|
||||||
|
elif [[ "${url}" =~ ^https?:\/\/([a-zA-Z0-9_.\-]+)(:[0-9]+)?\/([a-zA-Z0-9_\-]+)/([a-zA-Z0-9_\-]+)(.git)?$ ]]; then
|
||||||
|
forge="${BASH_REMATCH[1],,}"
|
||||||
|
owner="${BASH_REMATCH[3],,}"
|
||||||
|
repo="${BASH_REMATCH[4],,}"
|
||||||
|
else
|
||||||
|
echo "[!] Do not know how to handle ${url}!" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
git clone "${url}" "${WORKSPACE_DIR}/${forge}/${owner}/${repo}"
|
||||||
|
}
|
||||||
|
|
||||||
|
_clone "$@"
|
||||||
112
stowers/shell-utils/dot-local/bin/toolbx
Executable file
112
stowers/shell-utils/dot-local/bin/toolbx
Executable file
|
|
@ -0,0 +1,112 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
TOOLBX_NAME="toolbx"
|
||||||
|
LIB_HOME="${HOME}/.local/share/eyedevelop/shell-utils/toolbx"
|
||||||
|
|
||||||
|
delete_toolbox() {
|
||||||
|
podman rm -f "${TOOLBX_NAME}"
|
||||||
|
}
|
||||||
|
|
||||||
|
create_toolbox() {
|
||||||
|
local image podman_args
|
||||||
|
image="debian:bookworm"
|
||||||
|
podman_args=(
|
||||||
|
"--name=${TOOLBX_NAME}"
|
||||||
|
"--hostuser=${USER}"
|
||||||
|
"--env=_REAL_USER=${USER}"
|
||||||
|
"--userns=keep-id"
|
||||||
|
"--security-opt=label=disable"
|
||||||
|
"--network=host"
|
||||||
|
"--hostname=toolbox"
|
||||||
|
"--volume=${HOME}:/home/${USER}"
|
||||||
|
"--user=root"
|
||||||
|
"--entrypoint=/entrypoint.sh"
|
||||||
|
)
|
||||||
|
|
||||||
|
while [[ "$#" -ge 1 ]]; do
|
||||||
|
case "$1" in
|
||||||
|
-i|--image)
|
||||||
|
image="$2"
|
||||||
|
shift
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
|
||||||
|
-p|--privileged)
|
||||||
|
podman_args+=("--privileged")
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
podman_args+=("$image")
|
||||||
|
|
||||||
|
if podman container exists toolbx; then
|
||||||
|
echo "[!] Toolbx container already exists. Want to replace?"
|
||||||
|
echo -n "(Y/n) > "
|
||||||
|
read -r opt
|
||||||
|
|
||||||
|
if [[ "${opt,,}" == "n" ]]; then
|
||||||
|
echo "[!] Aborting."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
delete_toolbox
|
||||||
|
fi
|
||||||
|
|
||||||
|
podman create "${podman_args[@]}" >/dev/null
|
||||||
|
podman cp "${LIB_HOME}/container_setup.sh" "${TOOLBX_NAME}":/entrypoint.sh >/dev/null
|
||||||
|
podman cp "${LIB_HOME}/container_autoshell.sh" "${TOOLBX_NAME}":/autoshell.sh &>/dev/null
|
||||||
|
podman start "${TOOLBX_NAME}" >/dev/null
|
||||||
|
|
||||||
|
echo "[+] Created!"
|
||||||
|
}
|
||||||
|
|
||||||
|
enter_toolbox() {
|
||||||
|
if ! podman container exists "${TOOLBX_NAME}"; then
|
||||||
|
create_toolbox
|
||||||
|
fi
|
||||||
|
|
||||||
|
podman exec -it "${TOOLBX_NAME}" "/autoshell.sh" "${SHELL}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_usage() {
|
||||||
|
echo "--- Primitive container-based toolbox utility ---"
|
||||||
|
echo "Usage: $0 <command> <args>"
|
||||||
|
echo "Commands:"
|
||||||
|
echo " enter -- Enter the toolbox."
|
||||||
|
echo " create [opts] [command...] -- Create a new toolbox."
|
||||||
|
echo " -i|--image -- The image to use to create the toolbox with."
|
||||||
|
echo " -p|--privileged -- Whether the toolbox is privileged or not."
|
||||||
|
echo " delete -- Delete the current toolbox."
|
||||||
|
echo
|
||||||
|
echo "If no command is specified, enter is assumed."
|
||||||
|
}
|
||||||
|
|
||||||
|
if [[ "$#" -lt 1 ]]; then
|
||||||
|
print_usage
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
while [[ "$#" -ge 1 ]]; do
|
||||||
|
COMMAND="$1"; shift
|
||||||
|
|
||||||
|
case "${COMMAND}" in
|
||||||
|
enter)
|
||||||
|
enter_toolbox "$@"
|
||||||
|
;;
|
||||||
|
|
||||||
|
create)
|
||||||
|
create_toolbox "$@"
|
||||||
|
;;
|
||||||
|
|
||||||
|
delete)
|
||||||
|
delete_toolbox "$@"
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
enter_toolbox "$@"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
REAL_SHELL="$1"
|
||||||
|
|
||||||
|
install_shell() {
|
||||||
|
source /etc/os-release
|
||||||
|
|
||||||
|
case "${ID}" in
|
||||||
|
debian)
|
||||||
|
apt-get -y update && apt-get -y install "$(apt-file update &>/dev/null && apt-file search -l "${REAL_SHELL}" | head -n1 )"
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
echo "[!] Unsupported distro: ${NAME}" >&2
|
||||||
|
return 1
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
start_shell() {
|
||||||
|
if command -v "${REAL_SHELL}" &>/dev/null; then
|
||||||
|
exec su -s "${REAL_SHELL}" - "${_REAL_USER}"
|
||||||
|
else
|
||||||
|
exec su -s "/bin/bash" - "${_REAL_USER}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
install_shell >/dev/null || true
|
||||||
|
start_shell "$@"
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
trap 'exit 0' SIGTERM SIGINT
|
||||||
|
|
||||||
|
if [[ "$#" -gt 1 ]] && [[ "$1" == /autoshell.sh ]]; then
|
||||||
|
exec /autoshell.sh "$@"
|
||||||
|
fi
|
||||||
|
|
||||||
|
install_deps() {
|
||||||
|
if [[ -f /.pkg-installed ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ! -f /etc/os-release ]]; then
|
||||||
|
echo "[!] Unsupported container image!" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
source /etc/os-release
|
||||||
|
case "${ID}" in
|
||||||
|
debian)
|
||||||
|
apt-get -y update && apt-get -y install \
|
||||||
|
sudo iproute2 man bash-completion net-tools git passwd util-linux apt-utils apt-file
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
echo "[!] Unsupported distro: ${NAME}" >&2
|
||||||
|
return 1
|
||||||
|
esac
|
||||||
|
|
||||||
|
touch /.pkg-installed
|
||||||
|
}
|
||||||
|
|
||||||
|
create_user() {
|
||||||
|
echo "${_REAL_USER} ALL=(root) NOPASSWD: ALL" > /etc/sudoers.d/toolbx
|
||||||
|
}
|
||||||
|
|
||||||
|
install_deps
|
||||||
|
create_user
|
||||||
|
|
||||||
|
echo "127.0.1.1 $(hostname -f) $(hostname)" >> /etc/hosts
|
||||||
|
echo "[!] Done with setup!"
|
||||||
|
|
||||||
|
while true; do
|
||||||
|
sleep 86400
|
||||||
|
done
|
||||||
|
|
@ -2,3 +2,9 @@
|
||||||
|
|
||||||
alias vi="nvim"
|
alias vi="nvim"
|
||||||
alias vim="nvim"
|
alias vim="nvim"
|
||||||
|
alias cdw="cd \${WORKSPACE_DIR}"
|
||||||
|
|
||||||
|
function cdp() {
|
||||||
|
cd -- "$(_cdp_path "$@")"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,60 +0,0 @@
|
||||||
#!/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}"
|
|
||||||
}
|
|
||||||
Loading…
Add table
Reference in a new issue