forked from 89luca89/distrobox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistrobox-enter
executable file
·117 lines (106 loc) · 2.63 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
#!/bin/sh
# POSIX
# Expected env variables:
# HOME
# USER
# SHELL
trap '[ "$?" -ne 0 ] && echo An error occurred' EXIT
# We depend on podman let's be sure we have it
if ! command -v podman >/dev/null; then
echo "Missing dependency: podman"
exit 127
fi
# Defaults
verbose=0
container_name="fedora-toolbox-35"
container_command="${SHELL} -l"
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
echo "USAGE:
distrobox-enter --name fedora-toolbox-35 -- bash -l
Arguments:
--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
-v: show more verbosity
"
}
# Parse arguments
while :; do
case $1 in
-h | --help)
# Call a "show_help" function to display a synopsis, then exit.
show_help
exit
;;
-v)
shift
verbose=1
;;
-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 -q "${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
echo "" 1>&2
echo "Cannot start container, does it exist?" 1>&2
echo "Try running first:" 1>&2
echo "" 1>&2
echo " distrobox-create --name <name-of-container> --image <remote>/<docker>:<tag>" 1>&2
exit 1
else
echo >&2 "Starting container"
echo >&2 "run this command to follow along:"
echo >&2 " podman logs -f ${container_name}"
while ! podman logs "${container_name}" 2>/dev/null | grep -q "container_setup_done"; do
printf >&2 "."
sleep 1
done
echo >&2 "done!"
fi
fi
# entering container using our user and workdir
echo "podman exec --interactive --tty --user=${USER} --workdir=${HOME}"
echo "--env=DISTROBOX_ENTER_PATH=$(command -v distrobox-enter)"
# exporting current environment to container
for i in $(printenv | grep '=' | head -n -2); do
echo "--env=$i"
done
# run selected pod with command+args
echo "${container_name} ${container_command}"
}
# Generate the command and execute
$(generate_command)