refactor: small optimalizations

This commit is contained in:
Job
2026-03-15 12:37:34 +01:00
parent 16841bb334
commit 0089c971f4

18
devc.sh
View File

@@ -5,7 +5,7 @@
# different dev containers, resuming sessions and #
# automatic container updates. #
# =============================================== #
set -eu
set -euo pipefail
log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-}" "$1"; }
arg() { echo -n " $@"; }
@@ -69,9 +69,8 @@ param_args() {
arg "-e XAUTHORITY=/run/user/1000/.Xauthority"
;;
-mnt) # Mount directory.
arg "-w /mnt/ -v $2:/mnt/$([ ! -d "$2" ] && echo 'file')"
shift
arg "-w /mnt/"
arg "-v $1:/mnt/$([ ! -d "$1" ] && echo 'file')"
;;
*) # Use unknown arguments as container arguments.
arg "$1" ;;
@@ -101,24 +100,21 @@ name="${image%:*}"
# Get container registry from the DEVC_REGISTRY env
# variable.
if [ -n "${DEVC_REGISTRY:-}" ]; then
registry="$DEVC_REGISTRY"
else
registry="${DEVC_REGISTRY:-}"
if [ -z "$registry" ]; then
log "registry unknown; set the DEVC_REGISTRY environment variable" 'x' 31
exit 1
fi
# Get container command from the DEVC_COMMAND env variable
# if set, else use bash -l.
if [ -z "${DEVC_COMMAND:-}" ]; then
DEVC_COMMAND="bash -l"
fi
DEVC_COMMAND="${DEVC_COMMAND:-bash -l}"
# When container is not running or arguments are provided,
# recreate it.
if [ "$($engine container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ] || [[ $# -gt 0 ]]; then
log "starting devcontainer..."
$engine network create --ignore "devc"
log "starting $image..."
$engine network create --ignore "devc" 1>/dev/null
$engine container rm -f -t 0 "$name" 1>/dev/null
$engine run -td $(default_args) $(param_args $@) "$registry/$image"
fi