diff --git a/stowers/bash/dot-bashrc b/stowers/bash/dot-bashrc index baf4643..72a2e9e 100644 --- a/stowers/bash/dot-bashrc +++ b/stowers/bash/dot-bashrc @@ -30,3 +30,15 @@ unset rc if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ] && [ -z "$SSH_CONNECTION" ]; then exec tmux 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 "$@")" +} diff --git a/stowers/shell-utils/config.sh b/stowers/shell-utils/config.sh new file mode 100644 index 0000000..5ba72b4 --- /dev/null +++ b/stowers/shell-utils/config.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +COMMAND_DEPS=("bash") diff --git a/stowers/shell-utils/dot-local/bin/_cdp_path b/stowers/shell-utils/dot-local/bin/_cdp_path new file mode 100755 index 0000000..7263049 --- /dev/null +++ b/stowers/shell-utils/dot-local/bin/_cdp_path @@ -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 "$@" diff --git a/stowers/shell-utils/dot-local/bin/clone b/stowers/shell-utils/dot-local/bin/clone new file mode 100755 index 0000000..9535643 --- /dev/null +++ b/stowers/shell-utils/dot-local/bin/clone @@ -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 "$@" diff --git a/stowers/shell-utils/dot-local/bin/toolbx b/stowers/shell-utils/dot-local/bin/toolbx new file mode 100755 index 0000000..409828f --- /dev/null +++ b/stowers/shell-utils/dot-local/bin/toolbx @@ -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 " + 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 diff --git a/stowers/shell-utils/dot-local/share/eyedevelop/shell-utils/toolbx/container_autoshell.sh b/stowers/shell-utils/dot-local/share/eyedevelop/shell-utils/toolbx/container_autoshell.sh new file mode 100755 index 0000000..c7e622f --- /dev/null +++ b/stowers/shell-utils/dot-local/share/eyedevelop/shell-utils/toolbx/container_autoshell.sh @@ -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 "$@" diff --git a/stowers/shell-utils/dot-local/share/eyedevelop/shell-utils/toolbx/container_setup.sh b/stowers/shell-utils/dot-local/share/eyedevelop/shell-utils/toolbx/container_setup.sh new file mode 100755 index 0000000..73cbda0 --- /dev/null +++ b/stowers/shell-utils/dot-local/share/eyedevelop/shell-utils/toolbx/container_setup.sh @@ -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 diff --git a/stowers/zsh/dot-zshrc.d/01-aliases.sh b/stowers/zsh/dot-zshrc.d/01-aliases.sh index 2b7723b..ad4c7b5 100644 --- a/stowers/zsh/dot-zshrc.d/01-aliases.sh +++ b/stowers/zsh/dot-zshrc.d/01-aliases.sh @@ -2,3 +2,9 @@ alias vi="nvim" alias vim="nvim" +alias cdw="cd \${WORKSPACE_DIR}" + +function cdp() { + cd -- "$(_cdp_path "$@")" +} + diff --git a/stowers/zsh/dot-zshrc.d/10-workspace.sh b/stowers/zsh/dot-zshrc.d/10-workspace.sh deleted file mode 100644 index 5612083..0000000 --- a/stowers/zsh/dot-zshrc.d/10-workspace.sh +++ /dev/null @@ -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}" -}