mirror of
https://github.com/89luca89/distrobox.git
synced 2026-08-19 01:14:49 -05:00
139 lines
3.1 KiB
Bash
Executable File
139 lines
3.1 KiB
Bash
Executable File
#!/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} -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}
|
|
|
|
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 -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
|
|
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"
|
|
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} --workdir=${HOME}"
|
|
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
|
|
# shellcheck disable=SC2046
|
|
eval $(generate_command)
|