forked from Job79/devcontainer
refactor: small optimalizations
This commit is contained in:
18
devc.sh
18
devc.sh
@@ -5,7 +5,7 @@
|
|||||||
# different dev containers, resuming sessions and #
|
# different dev containers, resuming sessions and #
|
||||||
# automatic container updates. #
|
# automatic container updates. #
|
||||||
# =============================================== #
|
# =============================================== #
|
||||||
set -eu
|
set -euo pipefail
|
||||||
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 " $@"; }
|
||||||
|
|
||||||
@@ -69,9 +69,8 @@ param_args() {
|
|||||||
arg "-e XAUTHORITY=/run/user/1000/.Xauthority"
|
arg "-e XAUTHORITY=/run/user/1000/.Xauthority"
|
||||||
;;
|
;;
|
||||||
-mnt) # Mount directory.
|
-mnt) # Mount directory.
|
||||||
|
arg "-w /mnt/ -v $2:/mnt/$([ ! -d "$2" ] && echo 'file')"
|
||||||
shift
|
shift
|
||||||
arg "-w /mnt/"
|
|
||||||
arg "-v $1:/mnt/$([ ! -d "$1" ] && echo 'file')"
|
|
||||||
;;
|
;;
|
||||||
*) # Use unknown arguments as container arguments.
|
*) # Use unknown arguments as container arguments.
|
||||||
arg "$1" ;;
|
arg "$1" ;;
|
||||||
@@ -101,24 +100,21 @@ name="${image%:*}"
|
|||||||
|
|
||||||
# Get container registry from the DEVC_REGISTRY env
|
# Get container registry from the DEVC_REGISTRY env
|
||||||
# variable.
|
# variable.
|
||||||
if [ -n "${DEVC_REGISTRY:-}" ]; then
|
registry="${DEVC_REGISTRY:-}"
|
||||||
registry="$DEVC_REGISTRY"
|
if [ -z "$registry" ]; then
|
||||||
else
|
|
||||||
log "registry unknown; set the DEVC_REGISTRY environment variable" 'x' 31
|
log "registry unknown; set the DEVC_REGISTRY environment variable" 'x' 31
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get container command from the DEVC_COMMAND env variable
|
# Get container command from the DEVC_COMMAND env variable
|
||||||
# if set, else use bash -l.
|
# if set, else use bash -l.
|
||||||
if [ -z "${DEVC_COMMAND:-}" ]; then
|
DEVC_COMMAND="${DEVC_COMMAND:-bash -l}"
|
||||||
DEVC_COMMAND="bash -l"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# When container is not running or arguments are provided,
|
# When container is not running or arguments are provided,
|
||||||
# recreate it.
|
# recreate it.
|
||||||
if [ "$($engine 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 $image..."
|
||||||
$engine network create --ignore "devc"
|
$engine network create --ignore "devc" 1>/dev/null
|
||||||
$engine container rm -f -t 0 "$name" 1>/dev/null
|
$engine container rm -f -t 0 "$name" 1>/dev/null
|
||||||
$engine run -td $(default_args) $(param_args $@) "$registry/$image"
|
$engine run -td $(default_args) $(param_args $@) "$registry/$image"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user