Skip to content

Commit

Permalink
stop: align flags with rm. Add -a/--all flag. Fix #717, Fix #436
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Di Maio <[email protected]>
  • Loading branch information
89luca89 committed Aug 4, 2023
1 parent 9c8e91f commit afddcb9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
4 changes: 2 additions & 2 deletions distrobox-rm
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fi

# Declare it AFTER config sourcing because we do not want a default name set for rm.
container_name=""
container_name_default="my-distrobox"
container_name_list=""

# Print usage to stdout.
Expand Down Expand Up @@ -249,8 +250,7 @@ fi

# check if we have containers to delete
if [ -z "${container_name_list}" ]; then
printf >&2 "Please specify a container name.\n"
exit 1
container_name_list="${container_name_default}"
fi

delete_container() {
Expand Down
47 changes: 30 additions & 17 deletions distrobox-stop
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ if [ -n "${SUDO_USER}" ] || [ -n "${DOAS_USER}" ]; then
fi

# Defaults
all=0
container_manager="autodetect"
distrobox_flags=""
distrobox_path="$(dirname "$(realpath "${0}")")"
container_name=""
container_name_default="my-distrobox"
container_name_list=""
non_interactive=0
# If the user runs this script as root in a login shell, set rootful=1.
# There's no need for them to pass the --root flag option in such cases.
Expand Down Expand Up @@ -100,7 +104,7 @@ Usage:
Options:
--name/-n: name for the distrobox
--all/-a: stop all distroboxes
--yes/-Y: non-interactive, stop without asking
--help/-h: show this message
--root/-r: launch podman/docker with root privileges. Note that if you need root this is the preferred
Expand All @@ -119,6 +123,10 @@ while :; do
show_help
exit 0
;;
-a | --all)
shift
all=1
;;
-r | --root)
shift
rootful=1
Expand All @@ -131,13 +139,6 @@ while :; do
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
Expand All @@ -155,7 +156,7 @@ while :; do
# If we have a flagless option and container_name is not specified
# then let's accept argument as container_name
if [ -n "$1" ]; then
container_name="$1"
container_name_list="${container_name_list} $1"
shift
else
break
Expand Down Expand Up @@ -216,19 +217,31 @@ fi
# prepend sudo (or the specified sudo program) if we want podman or docker to be rootful
if [ "${rootful}" -ne 0 ]; then
container_manager="${distrobox_sudo_program-} ${container_manager}"
distrobox_flags="--root"
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
# If all, just set container_name to the list of names in distrobox-list
if [ "${all}" -ne 0 ]; then
# prepend sudo (or the specified sudo program) if we want podman or docker to be rootful
# shellcheck disable=SC2086,2248
container_name_list="$("${distrobox_path}"/distrobox-list ${distrobox_flags} --no-color |
tail -n +2 | cut -d'|' -f2 | tr -d ' ' | tr '\n' ' ')"
fi

if [ -z "${container_name_list}" ] && [ "${all}" -ne 0 ]; then
printf >&2 "No containers found.\n"
exit 0
fi

# check if we have containers to delete
if [ -z "${container_name_list}" ]; then
container_name_list="${container_name_default}"
fi


if [ "${non_interactive}" -eq 0 ]; then
# Prompt to stop the container.
printf "Do you really want to stop %s? [Y/n]: " "${container_name}"
printf "Do you really want to stop %s? [Y/n]: " "${container_name_list}"
read -r response
response="${response:-"Y"}"
else
Expand All @@ -239,7 +252,7 @@ fi
case "${response}" in
y | Y | Yes | yes | YES)
# Stop the container
${container_manager} stop "${container_name}"
${container_manager} stop ${container_name_list}
;;
n | N | No | no | NO)
printf "Aborted.\n"
Expand Down
5 changes: 3 additions & 2 deletions docs/usage/distrobox-stop.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ subsequent enters are really quick. This is how they can be stopped.

**distrobox stop**

--name/-n: name for the distrobox
--all/-a: stop all distroboxes
--yes/-Y: non-interactive, stop without asking
--help/-h: show this message
--root/-r: launch podman/docker with root privileges. Note that if you need root this is the preferred
Expand All @@ -26,8 +26,9 @@ subsequent enters are really quick. This is how they can be stopped.

# EXAMPLES

distrobox-stop --name container-name
distrobox-stop container-name1 container-name2
distrobox-stop container-name
distrobox-stop --all

You can also use environment variables to specify container manager and name:

Expand Down

0 comments on commit afddcb9

Please sign in to comment.