-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevbox
More file actions
66 lines (57 loc) · 1.56 KB
/
Copy pathdevbox
File metadata and controls
66 lines (57 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
set -euo pipefail
script_dir="$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
repo_root="${script_dir}"
image_name="${DEVBOX_IMAGE:-devbox-shell}"
home_volume="${DEVBOX_HOME_VOLUME:-devbox-home}"
dockerfile="${repo_root}/Dockerfile"
workspace_source="${PWD}"
workspace_link_dir=""
build_image() {
docker build \
--pull \
--tag "${image_name}" \
--file "${dockerfile}" \
"${repo_root}"
}
cleanup() {
if [[ -n "${workspace_link_dir}" ]]; then
rm -rf "${workspace_link_dir}"
fi
}
prepare_workspace_source() {
# Docker parses --mount as comma-separated key=value fields. When the
# working directory contains commas or line breaks, bind through a safe
# temporary symlink instead of interpolating the path directly.
case "${PWD}" in
*","*|*$'\n'*|*$'\r'*)
workspace_link_dir="$(mktemp -d "${TMPDIR:-/tmp}/devbox-workspace.XXXXXX")"
ln -s "${PWD}" "${workspace_link_dir}/workspace"
workspace_source="${workspace_link_dir}/workspace"
trap cleanup EXIT
;;
esac
}
if [[ "${1:-}" == "--rebuild" ]]; then
build_image
shift
elif ! docker image inspect "${image_name}" >/dev/null 2>&1; then
build_image
fi
prepare_workspace_source
if [[ $# -eq 0 ]]; then
set -- bash
elif [[ "${1}" == -* ]]; then
set -- bash "$@"
fi
tty_args=()
if [[ -t 0 && -t 1 ]]; then
tty_args=(-it)
fi
docker run --rm \
"${tty_args[@]}" \
--mount "type=bind,src=${workspace_source},dst=/workspace" \
--mount "type=volume,src=${home_volume},dst=/home/dev" \
--workdir /workspace \
"${image_name}" \
"$@"