From bde737b037411c44959332e00d6287e385d0b432 Mon Sep 17 00:00:00 2001 From: Hans Goor Date: Sat, 23 Nov 2024 16:58:26 +0100 Subject: [PATCH] Add devcontainer creation alias with nvim config mount. Also add devc-ssh command. --- stowers/shell-utils/dot-local/bin/devc-ssh | 27 ++++++++++++++++++++++ stowers/zsh/dot-bash_completion | 9 ++++++++ stowers/zsh/dot-zshrc | 5 ++++ stowers/zsh/dot-zshrc.d/01-aliases.sh | 2 ++ 4 files changed, 43 insertions(+) create mode 100755 stowers/shell-utils/dot-local/bin/devc-ssh create mode 100644 stowers/zsh/dot-bash_completion diff --git a/stowers/shell-utils/dot-local/bin/devc-ssh b/stowers/shell-utils/dot-local/bin/devc-ssh new file mode 100755 index 0000000..6f38efd --- /dev/null +++ b/stowers/shell-utils/dot-local/bin/devc-ssh @@ -0,0 +1,27 @@ +#!/bin/bash + +CONTAINER="docker" +if ! command -v docker &>/dev/null; then + CONTAINER="podman" +fi + +if [[ "$#" -lt 1 ]]; then + echo "Usage: $0 " >&2 + exit 1 +fi + +container_id="$1"; shift + +# We expect a devcontainer to run an SSH server on 2222/tcp +# and to have this port exposed. We find the host binding +# to SSH to that container. + +if ! ssh_port="$(${CONTAINER} inspect --format '{{ (index (index .NetworkSettings.Ports "2222/tcp") 0).HostPort }}' "${container_id}" 2>/dev/null)"; then + echo "[!] Could not get SSH host port for container '${container_id}'" >&2 + exit 1 +fi + +ssh -o UserKnownHostsFile=/dev/null \ + -o StrictHostKeyChecking=false \ + -p "${ssh_port}" \ + dev@localhost "$@" diff --git a/stowers/zsh/dot-bash_completion b/stowers/zsh/dot-bash_completion new file mode 100644 index 0000000..5b71754 --- /dev/null +++ b/stowers/zsh/dot-bash_completion @@ -0,0 +1,9 @@ +#!/bin/bash + +_devc_ssh() { + if [[ "${#COMP_WORDS[@]}" -eq 2 ]]; then + COMPREPLY=( $(compgen -W "$(docker ps --format '{{ .Names }}' | tr '\n' ' ')" -- "${cur}" ) ) + fi +} + +complete -F _devc_ssh devc-ssh diff --git a/stowers/zsh/dot-zshrc b/stowers/zsh/dot-zshrc index ffeebf5..c041392 100644 --- a/stowers/zsh/dot-zshrc +++ b/stowers/zsh/dot-zshrc @@ -112,3 +112,8 @@ if [[ -d "${HOME}/.zshrc.d" ]]; then source "${ext}" done fi + +# Custom command completions +if [[ -f "${HOME}/.bash_completion" ]]; then + source "${HOME}"/.bash_completion +fi diff --git a/stowers/zsh/dot-zshrc.d/01-aliases.sh b/stowers/zsh/dot-zshrc.d/01-aliases.sh index ad4c7b5..f5884dd 100644 --- a/stowers/zsh/dot-zshrc.d/01-aliases.sh +++ b/stowers/zsh/dot-zshrc.d/01-aliases.sh @@ -8,3 +8,5 @@ function cdp() { cd -- "$(_cdp_path "$@")" } +alias devcontainer-nvim="devcontainer up --mount 'type=bind,source=${HOME}/.config/nvim,target=/home/dev/.config/nvim' --workspace-folder ." +