refactor: make script more portable

This commit is contained in:
Job
2026-02-21 11:07:21 +01:00
parent 2e8375d34f
commit e2b563270c

26
devc.sh
View File

@@ -9,7 +9,7 @@ set -eu
log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-}" "$1"; } log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-}" "$1"; }
arg() { echo -n " $@"; } arg() { echo -n " $@"; }
# run_args returns the podman run arguments required for # run_args returns the container run arguments required for
# starting a new container. # starting a new container.
default_args() { default_args() {
arg "--name $name" arg "--name $name"
@@ -23,7 +23,7 @@ default_args() {
# Disable selinux labeling so unix sockets can be mounted # Disable selinux labeling so unix sockets can be mounted
# without problems. # without problems.
arg "--security-opt label=disable" [ -d /sys/fs/selinux ] && arg "--security-opt label=disable"
# Mount the wayland socket. Required to get the system # Mount the wayland socket. Required to get the system
# clipboard (wl-copy) and gui applications working. # clipboard (wl-copy) and gui applications working.
@@ -42,7 +42,7 @@ default_args() {
[ -f "$config_file" ] && source "${config_file}" [ -f "$config_file" ] && source "${config_file}"
} }
# param_args returns the podman run arguments based on the # param_args returns the container run arguments based on the
# arguments provided to this script. # arguments provided to this script.
param_args() { param_args() {
while test $# -gt 0; do while test $# -gt 0; do
@@ -53,8 +53,11 @@ param_args() {
arg "-v /run/user/$UID/bus:/tmp/bus" arg "-v /run/user/$UID/bus:/tmp/bus"
arg "-e HOST_HOME=$HOME" # Used to translate paths. arg "-e HOST_HOME=$HOME" # Used to translate paths.
;; ;;
-podman-sock) # Mount podman sock into the container. -container-sock) # Mount container sock into the container.
arg "-v /run/user/$UID/podman/podman.sock:/var/run/docker.sock" case "$(basename "$engine")" in
podman) arg "-v /run/user/$UID/podman/podman.sock:/var/run/docker.sock" ;;
docker) arg "-v /var/run/docker.sock:/var/run/docker.sock" ;;
esac
;; ;;
-x11) # Enable X11 support. -x11) # Enable X11 support.
arg "-v /tmp/.X11-unix:/tmp/.X11-unix" arg "-v /tmp/.X11-unix:/tmp/.X11-unix"
@@ -67,7 +70,7 @@ param_args() {
arg "-w /mnt/" arg "-w /mnt/"
arg "-v $1:/mnt/$([ ! -d "$1" ] && echo 'file')" arg "-v $1:/mnt/$([ ! -d "$1" ] && echo 'file')"
;; ;;
*) # Use unknown arguments a podman arguments. *) # Use unknown arguments as container arguments.
arg "$1" ;; arg "$1" ;;
esac esac
shift shift
@@ -75,6 +78,9 @@ param_args() {
} }
### MAIN ### ### MAIN ###
engine="$(command -v podman || command -v docker)"
[ -z "$engine" ] && echo "no container engine found" && exit 1
# Get the devcontainer name from the first argument. If not # Get the devcontainer name from the first argument. If not
# provided, use the last used name when possible. # provided, use the last used name when possible.
if [[ $# -gt 0 ]] && [[ ${1:-} != -* ]]; then if [[ $# -gt 0 ]] && [[ ${1:-} != -* ]]; then
@@ -107,10 +113,10 @@ fi
# When container is not running or arguments are provided, # When container is not running or arguments are provided,
# recreate it. # recreate it.
if [ "$(podman container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ] || [[ $# -gt 0 ]]; then if [ "$($engine container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ] || [[ $# -gt 0 ]]; then
log "starting devcontainer..." log "starting devcontainer..."
podman container rm -f -t 0 "$name" 1>/dev/null $engine container rm -f -t 0 "$name" 1>/dev/null
podman run -td $(default_args) $(param_args $@) "$registry/$image" $engine run -td $(default_args) $(param_args $@) "$registry/$image"
fi fi
podman exec --detach-keys "ctrl-@,ctrl-@" -it "$name" ${DEVC_COMMAND:-} $engine exec --detach-keys "ctrl-@,ctrl-@" -it "$name" ${DEVC_COMMAND:-}