Files
distrobox/distrobox-stop
T

173 lines
4.4 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
# https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.
# POSIX
# Dont' run this command as sudo.
if [ "$(id -u)" -eq 0 ]; then
printf >&2 "Running %s as sudo is not supported.\n" "${0}"
printf >&2 "Please check the documentation on:\n"
printf >&2 "\tman distrobox-compatibility\t"
printf >&2 "or consult the documentation page on:\n"
printf >&2 "\thttps://github.com/89luca89/distrobox/blob/main/docs/compatibility.md\n"
exit 1
fi
# Defaults
container_name="${DB_CONTAINER_NAME:-""}"
non_interactive="${DBX_NON_INTERACTIVE:-0}"
verbose=0
version="1.2.14"
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
cat <<EOF
distrobox version: ${version}
Usage:
distrobox-stop --name container-name
distrobox-stop container-name
Options:
--name/-n: name for the distrobox
--yes/-Y: non-interactive, stop without asking
--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)
verbose=1
shift
;;
-V | --version)
printf "distrobox: %s\n" "${version}"
exit 0
;;
-n | --name)
if [ -n "$2" ]; then
container_name="$2"
shift
shift
fi
;;
-Y | --yes)
non_interactive=1
shift
;;
--) # End of all options.
shift
break
;;
*) # Default case: If no more options then break out of the loop.
# If we have a flagless option and container_name is not specified
# then let's accept argument as container_name
if [ -z "${container_name}" ] && [ -n "$1" ]; then
container_name="$1"
shift
else
break
fi
;;
esac
done
# Ensure the foundamental variables are set and not empty, we will not proceed if
# they are not all set.
[ -z "${container_name}" ] && printf "Error: Invalid arguments, missing container name\n" && exit 2
set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# We depend on a container manager let's be sure we have it
# First we use podman, else docker
container_manager="podman"
# Be sure we have a container manager to work with.
if ! command -v podman >/dev/null; then
# If no podman, try docker.
container_manager="docker"
if ! command -v docker >/dev/null; then
# Error: we need at least one between docker or podman.
printf >&2 "Missing dependency: we need a container manager.\n"
printf >&2 "Please install one of podman or docker.\n"
printf >&2 "You can follow the documentation on:\n"
printf >&2 "\tman distrobox-compatibility\n"
printf >&2 "or:\n"
printf >&2 "\thttps://github.com/89luca89/distrobox/blob/main/docs/compatibility.md\n"
exit 127
fi
fi
# add verbose if -v is specified
if [ "${verbose}" -ne 0 ]; then
container_manager="${container_manager} --log-level debug"
fi
# Inspect the container we're working with.
container_exists="$?"
# Does the container exist? check if inspect reported errors
if [ "${container_exists}" -gt 0 ]; then
printf >&2 "Cannot find container %s, does it exist?\n" "${container_name}"
exit 1
fi
if [ "${non_interactive}" -eq 0 ]; then
# Prompt to download it.
printf "Do you really want to stop %s? [Y/n]: " "${container_name}"
read -r response
response="${response:-"Y"}"
else
response="yes"
fi
# Accept only y,Y,Yes,yes,n,N,No,no.
case "${response}" in
y | Y | Yes | yes | YES)
# Stop the container
${container_manager} stop "${container_name}"
;;
n | N | No | no | NO)
printf "Aborted.\n"
exit 0
;;
*) # Default case: If no more options then break out of the loop.
printf >&2 "Invalid input.\n"
printf >&2 "The available choices are: y,Y,Yes,yes,YES or n,N,No,no,NO.\nExiting.\n"
exit 1
;;
esac