-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathdistrobox-enter
executable file
·147 lines (130 loc) · 3.55 KB
/
distrobox-enter
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
# POSIX
# Expected env variables:
# HOME
# USER
# SHELL
trap '[ "$?" -ne 0 ] && printf "An error occurred\n"' EXIT
# We depend on podman let's be sure we have it
if ! command -v podman >/dev/null; then
printf >&2 "Missing dependency: podman\n"
exit 127
fi
# Defaults
container_command="${SHELL:-"bash"} -l"
container_name="fedora-toolbox-35"
verbose=0
version="distrobox_version_placeholder"
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
cat <<EOF
distrobox version: ${version}
distrobox-enter takes care of entering the container with the name specified.
Default command executed is your SHELL, buf you can specify different shells or
entire commands to execute.
Usage:
distrobox-enter --name fedora-toolbox-35 -- bash -l
Options:
--name/-n: name for the distrobox default: fedora-toolbox-35
--/-e: end arguments execute the rest as command to execute at login default: bash -l
--help/-h: show this message
--verbose/-v: show more verbosity
--version/-V: show version
EOF
}
# Parse arguments
while :; do
case $1 in
-h | --help)
# Call a "show_help" function to display a synopsis, then exit.
show_help
exit 0
;;
-v | --verbose)
shift
verbose=1
;;
-V | --version)
printf "distrobox: %s\n" "${version}"
exit 0
;;
-n | --name)
if [ -n "$2" ]; then
container_name="$2"
shift
shift
fi
;;
-e | --exec | --)
shift
container_command=$*
break
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
done
set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# Generate Podman command to execute.
# Arguments:
# None
# Outputs:
# prints the podman command to enter the distrobox container
generate_command() {
# If the container is not already running, we need to start if first
if ! podman ps | grep -qE "(^| )${container_name}( |$)"; then
# if container is not running, start it first
if ! podman start "${container_name}" >/dev/null; then
# if cannot start, container not found!
# prompt to create it first
printf >&2 "%s\n" "
Cannot start container, does it exist?
Try running first:
distrobox-create --name <name-of-container> --image <remote>/<docker>:<tag>
"
exit 1
else
printf >&2 "%s\n" "
Starting container
run this command to follow along:
podman logs -f ${container_name}"
while ! podman logs "${container_name}" 2>/dev/null | grep -q "container_setup_done"; do
printf >&2 "."
sleep 1
done
printf >&2 "done!\n"
fi
fi
# entering container using our user and workdir
result_command="podman exec"
# add podman verbose if -v is specified
if [ "${verbose}" -ne 0 ]; then
result_command="${result_command} --log-level debug"
fi
result_command="${result_command} --interactive --tty"
result_command="${result_command} --user=${USER}"
# start container from working directory. Else default to home. Else do /.
result_command="${result_command} --workdir=${PWD:-${HOME:-"/"}}"
# pass distrobox-enter path, it will be used in the distrobox-export tool.
result_command="${result_command} --env=DISTROBOX_ENTER_PATH=$(command -v distrobox-enter)"
# exporting current environment to container
for i in $(printenv | grep '=' | grep -v ' '); do
result_command="${result_command} --env=\"${i}\""
done
# run selected pod with command+args
result_command="${result_command} ${container_name} ${container_command}"
printf "%s" "${result_command}"
}
# Generate the command and execute
cmd="$(generate_command)"
# shellcheck disable=SC2086
eval ${cmd}