From 2ca42d5addf1d7037c9ed0e1a0154efaa1db20c3 Mon Sep 17 00:00:00 2001 From: Job79 Date: Tue, 17 Mar 2026 20:26:33 +0100 Subject: [PATCH] refactor: simplify log, die and formatting --- devc.sh | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/devc.sh b/devc.sh index 810a7a6..fd0164a 100755 --- a/devc.sh +++ b/devc.sh @@ -4,8 +4,8 @@ # Dev container entry script. # # =============================================== # set -euo pipefail -log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-â—‹}" "$1"; } -die() { log "$1" 'x' 31 && exit 1; } +log() { echo -e "\e[36mâ—‹\e[0m $1"; } +die() { echo -e "\e[31mx\e[0m $1" && exit 1; } # default_args configures standard container options. default_args() { @@ -14,7 +14,6 @@ default_args() { "--hostname" "$name" "--pull=newer" # Update image. "--userns=keep-id" # Map host user. - "--net=devc" # Shared network. "-v" "$name:/home/user:copy" # Persistent home volume. ) @@ -36,17 +35,9 @@ param_args() { case "$1" in -gpu) run_opts+=("--device" "/dev/dri") ;; -host-spawn) run_opts+=("-v" "/run/user/$UID/bus:/tmp/bus" "-e" "HOST_HOME=$HOME") ;; - -container-sock) run_opts+=("-v" "${XDG_RUNTIME_DIR:-/run/user/$UID}/podman/podman.sock:/var/run/docker.sock") ;; - -x11) - run_opts+=("-v" "/tmp/.X11-unix:/tmp/.X11-unix" "-v" "${XAUTHORITY:-$HOME/.Xauthority}:/run/user/1000/.Xauthority:ro") - run_opts+=("-e" "DISPLAY=${DISPLAY:-:0}" "-e" "XAUTHORITY=/run/user/1000/.Xauthority") - ;; - -mnt) - local type='' - [[ ! -d "$2" ]] && type='file' - run_opts+=("-w" "/mnt/" "-v" "$2:/mnt/$type") - shift - ;; + -container-sock) run_opts+=("-v" "$XDG_RUNTIME_DIR/podman/podman.sock:/var/run/docker.sock") ;; + -x11) run_opts+=("-v" "/tmp/.X11-unix:/tmp/.X11-unix" "-v" "$XAUTHORITY:/run/user/1000/.Xauthority:ro" "-e" "DISPLAY=$DISPLAY" "-e" "XAUTHORITY=/run/user/1000/.Xauthority") ;; + -mnt) shift && run_opts+=("-w" "/mnt/" "-v" "$1:/mnt/$([[ -d "$1" ]] || echo "file")") ;; *) run_opts+=("$1") ;; esac shift @@ -60,8 +51,8 @@ main() { # Resolve container name (CLI arg > Last used > Error). if [[ $image && $image != -* ]]; then shift - [[ $image == *:* ]] || image+=":main" - mkdir -p "${state_file%/*}" && echo "$image" >"$state_file" + [[ $image != *:* ]] && image+=":main" + echo "$image" >"$state_file" elif [[ -f $state_file ]]; then image=$(<$state_file) else @@ -72,12 +63,11 @@ main() { # Start/Restart if not running or if arguments change configuration. if [[ -z "$(podman ps -q -f name="^$name$" -f status=running)" ]] || (($# > 0)); then log "starting $image..." - [[ -n "${DEVC_REGISTRY:-}" ]] || die "registry unknown; set the DEVC_REGISTRY environment variable" default_args param_args "$@" - podman network create --ignore "devc" &>/dev/null + [[ -n "${DEVC_REGISTRY:-}" ]] || die "registry unknown; set the DEVC_REGISTRY environment variable" podman run --replace --stop-timeout 0 -td "${run_opts[@]}" "$DEVC_REGISTRY/$image" fi