feat: add DEVC_COMMAND option
This commit is contained in:
35
devc.sh
35
devc.sh
@@ -33,13 +33,13 @@ default_args() {
|
||||
[ -e "$SSH_AUTH_SOCK" ] && arg "-v $SSH_AUTH_SOCK:/run/user/1000/ssh-auth-sock"
|
||||
|
||||
# Make the user home dir a volume so it survives container
|
||||
# restarts. Use copy to keep the homedir files from the image.
|
||||
# restarts. Use copy to keep the files from the image.
|
||||
arg "-v $name:/home/user:copy"
|
||||
|
||||
# If there is custom configuration for the container, load
|
||||
# it here.
|
||||
config_file="$(dirname "$(realpath "$0")")/containers/$name/config.sh"
|
||||
[ -f "${config_file}" ] && source "${config_file}"
|
||||
[ -f "$config_file" ] && source "${config_file}"
|
||||
}
|
||||
|
||||
# param_args returns the podman run arguments based on the
|
||||
@@ -56,8 +56,8 @@ param_args() {
|
||||
;;
|
||||
-x11) # Enable X11 support.
|
||||
arg "-e DISPLAY=$DISPLAY"
|
||||
arg "-v /tmp/.X11-unix:/tmp/.X11-unix"
|
||||
arg "-e XAUTHORITY=/run/user/1000/.Xauthority"
|
||||
arg "-v /tmp/.X11-unix:/tmp/.X11-unix"
|
||||
arg "-v $XAUTHORITY:/run/user/1000/.Xauthority:ro"
|
||||
;;
|
||||
*) # Use unknown arguments a podman arguments.
|
||||
@@ -68,14 +68,6 @@ param_args() {
|
||||
}
|
||||
|
||||
### MAIN ###
|
||||
# Get container registry from DEVC_REGISTRY env variable.
|
||||
if [ -n "${DEVC_REGISTRY:-}" ]; then
|
||||
registry="$DEVC_REGISTRY"
|
||||
else
|
||||
log "registry unknown; set the DEVC_REGISTRY environment variable" 'x' 31
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Get the devcontainer name from the first argument. If not
|
||||
# provided, use the last used name when possible.
|
||||
if [[ $# -gt 0 ]] && [[ ${1:-} != -* ]]; then
|
||||
@@ -91,12 +83,27 @@ else
|
||||
fi
|
||||
name="${image%:*}"
|
||||
|
||||
# Create a new container when the container is not running or
|
||||
# when any arguments are provided.
|
||||
# Get container registry from the DEVC_REGISTRY env
|
||||
# variable.
|
||||
if [ -n "${DEVC_REGISTRY:-}" ]; then
|
||||
registry="$DEVC_REGISTRY"
|
||||
else
|
||||
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
|
||||
|
||||
# When container is not running or arguments are provided,
|
||||
# recreate it.
|
||||
if [ "$(podman container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ] || [[ $# -gt 0 ]]; then
|
||||
log "starting devcontainer..."
|
||||
podman container rm -f -t 0 "$name" 1>/dev/null
|
||||
podman run -td $(default_args) $(param_args $@) "$registry/$image"
|
||||
fi
|
||||
|
||||
podman exec -it "$name" bash -l
|
||||
podman exec -it "$name" ${DEVC_COMMAND:-}
|
||||
|
||||
Reference in New Issue
Block a user