mirror of
https://github.com/89luca89/distrobox.git
synced 2026-08-19 01:14:49 -05:00
As suggested in #252. Signed-off-by: Nathan Chancellor <nathan@kernel.org>
207 lines
5.8 KiB
Bash
Executable File
207 lines
5.8 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
|
|
# Optional env variables:
|
|
# DBX_CONTAINER_MANAGER
|
|
|
|
# Dont' run this command as sudo.
|
|
if [ "$(id -u)" -eq 0 ]; then
|
|
printf >&2 "Running %s as sudo is not supported.\n" "$(basename "${0}")"
|
|
printf >&2 " try instead running:\n"
|
|
printf >&2 " %s --root %s\n" "$(basename "${0}")" "$*"
|
|
exit 1
|
|
fi
|
|
|
|
# Defaults
|
|
rootful=0
|
|
size=0
|
|
verbose=0
|
|
version="1.2.16"
|
|
container_manager="autodetect"
|
|
|
|
# Source configuration files, this is done in an hierarchy so local files have
|
|
# priority over system defaults
|
|
# leave priority to environment variables.
|
|
config_files="
|
|
/usr/share/distrobox/distrobox.conf
|
|
/etc/distrobox/distrobox.conf
|
|
${HOME}/.config/distrobox/distrobox.conf
|
|
${HOME}/.distroboxrc
|
|
"
|
|
for config_file in ${config_files}; do
|
|
# shellcheck disable=SC1090
|
|
[ -e "${config_file}" ] && . "${config_file}"
|
|
done
|
|
[ -n "${DBX_CONTAINER_MANAGER}" ] && container_manager="${DBX_CONTAINER_MANAGER}"
|
|
|
|
# Print usage to stdout.
|
|
# Arguments:
|
|
# None
|
|
# Outputs:
|
|
# print usage with examples.
|
|
show_help() {
|
|
cat << EOF
|
|
distrobox version: ${version}
|
|
|
|
Usage:
|
|
|
|
distrobox-list
|
|
|
|
Options:
|
|
|
|
--help/-h: show this message
|
|
--root/-r: launch podman/docker with root privileges. Note that if you need root this is the preferred
|
|
way over "sudo distrobox"
|
|
--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
|
|
;;
|
|
-r | --root)
|
|
shift
|
|
rootful=1
|
|
;;
|
|
-s | --size)
|
|
size=1
|
|
shift
|
|
;;
|
|
-v | --verbose)
|
|
verbose=1
|
|
shift
|
|
;;
|
|
-V | --version)
|
|
printf "distrobox: %s\n" "${version}"
|
|
exit 0
|
|
;;
|
|
--) # End of all options.
|
|
shift
|
|
break
|
|
;;
|
|
-*) # Invalid options.
|
|
printf >&2 "ERROR: Invalid flag '%s'\n\n" "$1"
|
|
show_help
|
|
exit 1
|
|
;;
|
|
*) # 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
|
|
|
|
# We depend on a container manager let's be sure we have it
|
|
# First we use podman, else docker
|
|
case "${container_manager}" in
|
|
autodetect)
|
|
if command -v podman > /dev/null; then
|
|
container_manager="podman"
|
|
elif command -v docker > /dev/null; then
|
|
container_manager="docker"
|
|
else
|
|
container_manager="not_found"
|
|
fi
|
|
;;
|
|
podman)
|
|
container_manager="podman"
|
|
;;
|
|
docker)
|
|
container_manager="docker"
|
|
;;
|
|
*)
|
|
printf >&2 "Invalid input %s.\n" "${container_manager}"
|
|
printf >&2 "The available choices are: 'autodetect', 'podman', 'docker'\n"
|
|
container_manager="not_found"
|
|
;;
|
|
esac
|
|
|
|
# Be sure we have a container manager to work with.
|
|
if ! command -v "${container_manager}" > /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
|
|
# add verbose if -v is specified
|
|
if [ "${verbose}" -ne 0 ]; then
|
|
container_manager="${container_manager} --log-level debug"
|
|
fi
|
|
|
|
# prepend sudo if we want podman or docker to be rootful
|
|
if [ "${rootful}" -ne 0 ]; then
|
|
container_manager="sudo ${container_manager}"
|
|
fi
|
|
|
|
# List containers using custom format that inclused MOUNTS
|
|
# we do this as we can detect the custom mounts done by distrobox to distringuish
|
|
# between a normal podman or docker container and a distrobox one.
|
|
if [ "${size}" -ne 0 ]; then
|
|
container_list=$(${container_manager} ps -a --no-trunc --size --format \
|
|
"{{.ID}}|{{.Image}}|{{.Names}}|{{.State}}|{{.Mounts}}|{{.Size}}")
|
|
printf " %-12s | %-25s | %-30s | %-40s | %-30s\n" "ID" "NAME" "STATUS" "IMAGE" "SIZE"
|
|
else
|
|
container_list=$(${container_manager} ps -a --no-trunc --format \
|
|
"{{.ID}}|{{.Image}}|{{.Names}}|{{.State}}|{{.Mounts}}")
|
|
printf " %-12s | %-25s | %-30s | %-30s\n" "ID" "NAME" "STATUS" "IMAGE"
|
|
fi
|
|
IFS='
|
|
'
|
|
# Header of the output
|
|
for container in ${container_list}; do
|
|
|
|
# Check if the current container has a custom mount point for distrobox.
|
|
if [ -z "${container##*distrobox*}" ]; then
|
|
|
|
# Extract the information for the single container to pretty print it
|
|
container_id="$(printf "%s" "${container}" | cut -d'|' -f1 | cut -c1-12)"
|
|
container_image="$(printf "%s" "${container}" | cut -d'|' -f2)"
|
|
container_name="$(printf "%s" "${container}" | cut -d'|' -f3)"
|
|
container_status="$(printf "%s" "${container}" | cut -d'|' -f4)"
|
|
container_size="$(printf "%s" "${container}" | cut -d'|' -f6)"
|
|
|
|
# If the container is Up and Running, print it in green
|
|
if [ -z "${container_status##*Up*}" ] || [ -z "${container_status##*running*}" ]; then
|
|
printf "\033[32m %-12s | %-25s | %-30s | %-40s | %-30s\n\033[0m" \
|
|
"${container_id}" "${container_name}" "${container_status}" "${container_image}" "${container_size}"
|
|
# Else, print it in yellow
|
|
else
|
|
printf "\033[33m %-12s | %-25s | %-30s | %-40s | %-30s\n\033[0m" \
|
|
"${container_id}" "${container_name}" "${container_status}" "${container_image}" "${container_size}"
|
|
fi
|
|
fi
|
|
done
|