refactor: simplify log, die and formatting
All checks were successful
Build containers / changes (push) Successful in 3s
Build containers / base-image (push) Successful in 2m41s
Build containers / dependent-images (go) (push) Successful in 4s
Build containers / dependent-images (infra) (push) Successful in 2s
Build containers / dependent-images (php) (push) Successful in 2s

This commit is contained in:
Job
2026-03-17 20:26:33 +01:00
parent f895c11cbd
commit 2ca42d5add

26
devc.sh
View File

@@ -4,8 +4,8 @@
# Dev container entry script. # # Dev container entry script. #
# =============================================== # # =============================================== #
set -euo pipefail set -euo pipefail
log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-}" "$1"; } log() { echo -e "\e[36m○\e[0m $1"; }
die() { log "$1" 'x' 31 && exit 1; } die() { echo -e "\e[31mx\e[0m $1" && exit 1; }
# default_args configures standard container options. # default_args configures standard container options.
default_args() { default_args() {
@@ -14,7 +14,6 @@ default_args() {
"--hostname" "$name" "--hostname" "$name"
"--pull=newer" # Update image. "--pull=newer" # Update image.
"--userns=keep-id" # Map host user. "--userns=keep-id" # Map host user.
"--net=devc" # Shared network.
"-v" "$name:/home/user:copy" # Persistent home volume. "-v" "$name:/home/user:copy" # Persistent home volume.
) )
@@ -36,17 +35,9 @@ param_args() {
case "$1" in case "$1" in
-gpu) run_opts+=("--device" "/dev/dri") ;; -gpu) run_opts+=("--device" "/dev/dri") ;;
-host-spawn) run_opts+=("-v" "/run/user/$UID/bus:/tmp/bus" "-e" "HOST_HOME=$HOME") ;; -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") ;; -container-sock) run_opts+=("-v" "$XDG_RUNTIME_DIR/podman/podman.sock:/var/run/docker.sock") ;;
-x11) -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") ;;
run_opts+=("-v" "/tmp/.X11-unix:/tmp/.X11-unix" "-v" "${XAUTHORITY:-$HOME/.Xauthority}:/run/user/1000/.Xauthority:ro") -mnt) shift && run_opts+=("-w" "/mnt/" "-v" "$1:/mnt/$([[ -d "$1" ]] || echo "file")") ;;
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
;;
*) run_opts+=("$1") ;; *) run_opts+=("$1") ;;
esac esac
shift shift
@@ -60,8 +51,8 @@ main() {
# Resolve container name (CLI arg > Last used > Error). # Resolve container name (CLI arg > Last used > Error).
if [[ $image && $image != -* ]]; then if [[ $image && $image != -* ]]; then
shift shift
[[ $image == *:* ]] || image+=":main" [[ $image != *:* ]] && image+=":main"
mkdir -p "${state_file%/*}" && echo "$image" >"$state_file" echo "$image" >"$state_file"
elif [[ -f $state_file ]]; then elif [[ -f $state_file ]]; then
image=$(<$state_file) image=$(<$state_file)
else else
@@ -72,12 +63,11 @@ main() {
# Start/Restart if not running or if arguments change configuration. # Start/Restart if not running or if arguments change configuration.
if [[ -z "$(podman ps -q -f name="^$name$" -f status=running)" ]] || (($# > 0)); then if [[ -z "$(podman ps -q -f name="^$name$" -f status=running)" ]] || (($# > 0)); then
log "starting $image..." log "starting $image..."
[[ -n "${DEVC_REGISTRY:-}" ]] || die "registry unknown; set the DEVC_REGISTRY environment variable"
default_args default_args
param_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" podman run --replace --stop-timeout 0 -td "${run_opts[@]}" "$DEVC_REGISTRY/$image"
fi fi