diff --git a/distrobox-rm b/distrobox-rm index 4bbbdd0e73..a2a4faebf7 100755 --- a/distrobox-rm +++ b/distrobox-rm @@ -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. @@ -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() { diff --git a/distrobox-stop b/distrobox-stop index 86d238943e..9b18bca7d3 100755 --- a/distrobox-stop +++ b/distrobox-stop @@ -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. @@ -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 @@ -119,6 +123,10 @@ while :; do show_help exit 0 ;; + -a | --all) + shift + all=1 + ;; -r | --root) shift rootful=1 @@ -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 @@ -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 @@ -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 @@ -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" diff --git a/docs/usage/distrobox-stop.md b/docs/usage/distrobox-stop.md index 035e78ebf2..9b981d5325 100644 --- a/docs/usage/distrobox-stop.md +++ b/docs/usage/distrobox-stop.md @@ -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 @@ -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: