replace echo with printf and improve outputs

This commit is contained in:
89luca89
2021-12-10 09:08:06 +01:00
parent 06e9d27de4
commit da70f3b653
5 changed files with 134 additions and 116 deletions
+34 -30
View File
@@ -4,11 +4,11 @@
# HOME
# USER
trap '[ "$?" -ne 0 ] && echo An error occurred' EXIT
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
echo "Missing dependency: podman"
printf >&2 "Missing dependency: podman\n"
exit 127
fi
@@ -30,18 +30,20 @@ version="distrobox_version_placeholder"
# Outputs:
# print usage with examples.
show_help() {
echo "USAGE:
distrobox version: ${version}
cat <<EOF
distrobox version: ${version}
Usage:
distrobox-create --image registry.fedoraproject.org/fedora-toolbox:35 --name fedora-toolbox-35
Arguments:
--image/-i: image to use for the container default: registry.fedoraproject.org/fedora-toolbox:35
--name/-n: name for the distrobox default: fedora-toolbox-35
--help/-h: show this message
--verbose/-v: show more verbosity
--version/-V: show version
"
Options:
--image/-i: image to use for the container default: registry.fedoraproject.org/fedora-toolbox:35
--name/-n: name for the distrobox default: fedora-toolbox-35
--help/-h: show this message
--verbose/-v: show more verbosity
--version/-V: show version
"
EOF
}
# Parse arguments
@@ -57,7 +59,7 @@ while :; do
shift
;;
-V | --version)
echo "distrobox: ${version}"
printf "distrobox: %s\n" "${version}"
exit 0
;;
-i | --image)
@@ -98,11 +100,11 @@ fi
generate_command() {
# Set the container hostname the same as the container name.
# use the host's namespace for ipc, network, pid, ulimit
echo "podman create"
result_command="podman create"
if [ "${verbose}" -ne 0 ]; then
echo "--log-level debug"
result_command="${result_command} --log-level debug"
fi
echo "--dns none
result_command="${result_command} --dns none
--env=\"XDG_RUNTIME_DIR=/run/user/${container_user_uid}\"
--hostname ${container_name}
--ipc host"
@@ -111,7 +113,7 @@ generate_command() {
# able to syphon dynamic configurations from the host
#
# also mount the distrobox-init utility as the container entrypoint
echo "--name ${container_name}
result_command="${result_command} --name ${container_name}
--env=\"SHELL=${SHELL}\"
--network host
--no-hosts
@@ -125,10 +127,10 @@ generate_command() {
# let's check if we can include distrobox-export or not
if [ -n "${distrobox_export_path}" ]; then
echo "--volume ${distrobox_export_path}:/usr/bin/distrobox-export:ro"
result_command="${result_command} --volume ${distrobox_export_path}:/usr/bin/distrobox-export:ro"
fi
echo "--volume ${container_user_home}:${container_user_home}:rslave
result_command="${result_command} --volume ${container_user_home}:${container_user_home}:rslave
--volume /dev:/dev:rslave"
# useful mounts from host to the container using /run/host as a base
@@ -136,7 +138,7 @@ generate_command() {
for host_directory in ${host_directories}; do
# Check if the directory exists first
if [ -d "${host_directory}" ]; then
echo "--volume ${host_directory}:/run/host${host_directory}:rslave"
result_command="${result_command} --volume ${host_directory}:/run/host${host_directory}:rslave"
fi
done
# those are dynamic configs needed by the container to function properly
@@ -146,21 +148,21 @@ generate_command() {
# Check if the file exists first
if [ -f "${host_link}" ]; then
# Use realpath to not have multi symlink mess
echo "--volume $(realpath "${host_link}"):${host_link}:ro"
result_command="${result_command} --volume $(realpath "${host_link}"):${host_link}:ro"
fi
done
if [ -d /run/media ]; then
echo "--volume /run/media:/run/media:rslave"
result_command="${result_command} --volume /run/media:/run/media:rslave"
fi
# mount also the XDG_RUNTIME_DIR to ensure functionality of the apps
if [ -d /run/user/"${container_user_uid}" ]; then
echo "--volume /run/user/${container_user_uid}:/run/user/${container_user_uid}"
result_command="${result_command} --volume /run/user/${container_user_uid}:/run/user/${container_user_uid}"
fi
# mount devpts
if [ -f /dev/pts ]; then
echo "--mount type=devpts,destination=/dev/pts"
result_command="${result_command} --mount type=devpts,destination=/dev/pts"
fi
# find all the user's socket and mount them inside the container
@@ -169,23 +171,25 @@ generate_command() {
# the container
host_sockets="$(find /run -iname "*socket" ! -path "/run/user/*" 2>/dev/null || :)"
for socket in ${host_sockets}; do
echo "--volume ${socket}:${socket}"
result_command="${result_command} --volume ${socket}:${socket}"
done
# now execute the entrypoint, refer to `distrobox-init -h` for instructions
echo "${container_image}
result_command="${result_command} ${container_image}
/usr/bin/entrypoint -v --name ${container_user_name}
--user ${container_user_uid} --group ${container_user_gid}
--home ${container_user_home}"
printf "%s" "${result_command}"
}
# check that we have a complete distrobox installation or
# entrypoint and export will not work.
[ -z "${distrobox_entrypoint_path}" ] && echo "Error: no distrobox-init found in ${PATH}" && exit 127
[ -z "${distrobox_entrypoint_path}" ] && printf >&2 "Error: no distrobox-init found in %s\n" "${PATH}" && exit 127
# First, check if the image exists in the host
if ! podman image exists "${container_image}"; then
echo >&2 "Image not found."
printf >&2 "Image not found.\n"
printf >&2 "Do you want to pull the image now?[y/n] "
read -r response
# Accept only y,Y,Yes,yes,n,N,No,no.
@@ -195,12 +199,12 @@ if ! podman image exists "${container_image}"; then
podman pull "${container_image}"
;;
n | N | no | No)
echo >&2 "next time, run this command first:"
echo >&2 " podman pull ${container_image}"
printf >&2 "next time, run this command first:\n"
printf >&2 " podman pull %s\n" "${container_image}"
exit 0
;;
*) # Default case: If no more options then break out of the loop.
echo >&2 "The available choices are: y,Y,Yes,yes,n,N,No,no. Exiting."
printf >&2 "The available choices are: y,Y,Yes,yes,n,N,No,no. Exiting.\n"
exit 1
;;
esac
+35 -27
View File
@@ -5,11 +5,11 @@
# USER
# SHELL
trap '[ "$?" -ne 0 ] && echo An error occurred' EXIT
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
echo "Missing dependency: podman"
printf >&2 "Missing dependency: podman\n"
exit 127
fi
@@ -25,18 +25,20 @@ version="distrobox_version_placeholder"
# Outputs:
# print usage with examples.
show_help() {
echo "USAGE:
distrobox version: ${version}
cat <<EOF
distrobox version: ${version}
Usage:
distrobox-enter --name fedora-toolbox-35 -- bash -l
Arguments:
--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
"
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
@@ -52,7 +54,7 @@ while :; do
verbose=1
;;
-V | --version)
echo "distrobox: ${version}"
printf "distrobox: %s\n" "${version}"
exit 0
;;
-n | --name)
@@ -92,37 +94,43 @@ generate_command() {
# if cannot start, container not found!
# prompt to create it first
echo >&2 ""
echo >&2 "Cannot start container, does it exist?"
echo >&2 "Try running first:"
echo >&2 ""
echo >&2 " distrobox-create --name <name-of-container> --image <remote>/<docker>:<tag>"
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
echo >&2 "Starting container"
echo >&2 "run this command to follow along:"
echo >&2 " podman logs -f ${container_name}"
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
echo >&2 "done!"
printf >&2 "done!\n"
fi
fi
# entering container using our user and workdir
echo "podman exec"
result_command="podman exec"
if [ "${verbose}" -ne 0 ]; then
echo "--log-level debug"
result_command="${result_command} --log-level debug"
fi
echo "--interactive --tty --user=${USER} --workdir=${HOME}"
echo "--env=DISTROBOX_ENTER_PATH=$(command -v distrobox-enter)"
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
echo "--env=\"${i}\""
result_command="${result_command} --env=\"${i}\""
done
# run selected pod with command+args
echo "${container_name} ${container_command}"
result_command="${result_command} ${container_name} ${container_command}"
printf "%s" "${result_command}"
}
# Generate the command and execute
+24 -22
View File
@@ -5,7 +5,7 @@
# USER
# DISTROBOX_ENTER_PATH
trap '[ "$?" -ne 0 ] && echo An error occurred' EXIT
trap '[ "$?" -ne 0 ] && printf "An error occurred\n"' EXIT
# Defaults
exported_app=""
@@ -19,7 +19,7 @@ version="distrobox_version_placeholder"
base_dependencies="basename grep sed find"
for dep in ${base_dependencies}; do
if ! command -v "${dep}" >/dev/null; then
echo "Missing dependency: ${dep}"
printf >&2 "Missing dependency: %s\n" "${dep}"
exit 127
fi
done
@@ -30,23 +30,25 @@ done
# Outputs:
# print usage with examples.
show_help() {
echo "USAGE:
distrobox version: ${version}
cat <<EOF
distrobox version: ${version}
Usage:
distrobox-export --app mpv
distrobox-export --service syncthing
Note you can use --app OR --service but not together.
Note you can use --app OR --service but not together.
Arguments:
--app/-a: name of the application to export
--service/-s: name of the service to export
--delete/-d: delete exported application or service
--help/-h: show this message
--extra-flags/-ef: extra flags to add to the command
--verbose/-v: show more verbosity
--version/-V: show version
"
Options:
--app/-a: name of the application to export
--service/-s: name of the service to export
--delete/-d: delete exported application or service
--help/-h: show this message
--extra-flags/-ef: extra flags to add to the command
--verbose/-v: show more verbosity
--version/-V: show version
"
EOF
}
# Parse arguments
@@ -62,7 +64,7 @@ while :; do
verbose=1
;;
-V | --version)
echo "distrobox: ${version}"
printf "distrobox: %s\n" "${version}"
exit 0
;;
-a | --app)
@@ -97,18 +99,18 @@ done
# Ensure the foundamental variables are set and not empty, we will not proceed if
# they are not all set.
if [ -z "${exported_app}" ] && [ -z "${exported_service}" ]; then
echo "Invalid arguments, missing app or service to export"
printf >&2 "Invalid arguments, missing app or service to export\n"
exit 2
fi
if [ -n "${exported_app}" ] && [ -n "${exported_service}" ]; then
echo "Invalid arguments, choose an app OR a service to export"
printf >&2 "Invalid arguments, choose an app OR a service to export\n"
exit 2
fi
# Also check we're running inside a container and not on the host
if [ ! -f /run/.containerenv ]; then
echo "You must run $(basename "$0") inside a container..."
exit 2
printf >&2 "You must run %s inside a container...\n" " $(basename "$0")"
exit 126
fi
set -o errexit
@@ -126,7 +128,7 @@ container_command_prefix="${DISTROBOX_ENTER_PATH:-"distrobox-enter"} --name ${co
if [ -n "${exported_app}" ]; then
# Ensure the app we're exporting is installed
if ! command -v "${exported_app}" >/dev/null; then
echo "Error: trying to export a non-installed application"
printf >&2 "Error: trying to export a non-installed application\n"
exit 127
fi
# Find desktop file for the application to export
@@ -167,7 +169,7 @@ if [ -n "${exported_app}" ]; then
elif [ -n "${exported_service}" ]; then
# If we're managing services, let's be sure we have systemctl
if ! command -v systemctl >/dev/null; then
echo "Missing dependency: systemd"
printf >&2 "Missing dependency: systemd\n"
exit 127
fi
# Ensure we're working with fresh data
@@ -177,7 +179,7 @@ elif [ -n "${exported_service}" ]; then
# Create temp file with random name
temp_file="/tmp/$(
tr -dc A-Za-z0-9 </dev/urandom | head -c 13
echo ''
printf "\n"
)"
# Replace all Exec occurrencies
for cmd in ExecStart ExecStartPre ExecStartPost ExecReload ExecStop ExecStopPost; do
+30 -27
View File
@@ -5,7 +5,7 @@
# USER
# SHELL
trap '[ "$?" -ne 0 ] && echo An error occurred' EXIT
trap '[ "$?" -ne 0 ] && printf "An error occurred\n"' EXIT
# Defaults
verbose=0
@@ -17,20 +17,23 @@ version="distrobox_version_placeholder"
# Outputs:
# print usage with examples.
show_help() {
echo "USAGE:
distrobox version: ${version}
cat <<EOF
distrobox version: ${version}
Usage:
distrobox-init --name ${USER} --user $(id -ru) --group $(id -rg) --home ${HOME}
Arguments:
--name/-n: user name
--user/-u: uid of the user
--group/-g: gid of the user
--home/-d: path/to/home of the user
--help/-h: show this message
--verbose/-v: show more verbosity
--version/-V: show version
"
Options:
--name/-n: user name
--user/-u: uid of the user
--group/-g: gid of the user
--home/-d: path/to/home of the user
--help/-h: show this message
--verbose/-v: show more verbosity
--version/-V: show version
"
EOF
}
# Parse arguments
@@ -46,13 +49,13 @@ while :; do
verbose=1
;;
-V | --version)
echo "distrobox: ${version}"
printf "distrobox: %s\n" "${version}"
exit 0
;;
-n | --name)
if [ -n "$2" ]; then
container_user_name="$2"
shift
s${container_user_name}hift
shift
fi
;;
@@ -84,14 +87,14 @@ done
# Ensure the foundamental variables are set and not empty, we will not proceed if
# they are not all set.
[ -z "${container_user_name}" ] && echo "Invalid arguments, missing username" && exit 2
[ -z "${container_user_uid}" ] && echo "Invalid arguments, missing user uid" && exit 2
[ -z "${container_user_gid}" ] && echo "Invalid arguments, missing user gud" && exit 2
[ -z "${container_user_home}" ] && echo "Invalid argument, missing user home" && exit 2
[ -z "${container_user_name}" ] && printf >&2 "Invalid arguments, missing username\n" && exit 2
[ -z "${container_user_uid}" ] && printf >&2 "Invalid arguments, missing user uid\n" && exit 2
[ -z "${container_user_gid}" ] && printf >&2 "Invalid arguments, missing user gud\n" && exit 2
[ -z "${container_user_home}" ] && printf >&2 "Invalid argument, missing user home\n" && exit 2
# Also check we're running inside a container and not on the host
if [ ! -f /run/.containerenv ]; then
echo "You must run $(basename "$0") inside a container..."
printf >&2 "You must run %s inside a container...\n" " $(basename "$0")"
exit 126
fi
@@ -123,13 +126,13 @@ mount_bind() (
if [ -d "${source_dir}" ]; then
# exit if not successful
if ! mkdir -p "${target_dir}"; then
echo "Cannot create mount target directory: ${target_dir}"
printf >&2 "Cannot create mount target directory: %s\n" "${target_dir}"
return 1
fi
elif [ -f "${source_dir}" ]; then
# exit if not successful
if ! touch "${target_dir}"; then
echo "Cannot create mount target file: ${target_dir}"
printf >&2 "Cannot create mount target file: %s\n" "${target_dir}"
return 1
fi
fi
@@ -139,7 +142,7 @@ mount_bind() (
# bind mount source_dir to target_dir, return error if not successful
# shellcheck disable=SC2086
if ! mount --rbind ${mount_o} "${source_dir}" "${target_dir}"; then
echo "Failed to bind mount ${source_dir} to ${target_dir}"
printf >&2 "Failed to bind mount %s to %s\n" "${source_dir}" "${target_dir}"
return 1
fi
@@ -163,7 +166,7 @@ list_sudo_groups() (
group="wheel"
fi
echo "${group}"
printf "%s" "${group}"
return 0
)
@@ -190,7 +193,7 @@ if ! command -v mount || ! command -v passwd ||
elif command -v zypper; then
zypper install -y --no-recommends sudo shadow procps util-linux "${shell_pkg}"
else
echo "FAILED TO INSTALL BASE DEPENDENCIES"
printf >&2 "FAILED TO INSTALL BASE DEPENDENCIES\n"
# Exit as command not found
exit 127
fi
@@ -211,9 +214,9 @@ for host_mount in ${HOST_MOUNTS}; do
done
# Do not check fqdn when doing sudo, it will not work anyways
echo "Defaults !fqdn" >>/etc/sudoers
printf "Defaults !fqdn\n" >>/etc/sudoers
# Ensure passwordless sudo is set up for user
echo "${container_user_name} ALL = (root) NOPASSWD:ALL" >>/etc/sudoers
printf "%s ALL = (root) NOPASSWD:ALL\n" "${container_user_name}" >>/etc/sudoers
# Let's add our user to the container
# if the user already exists, just add it to the sudoers groups
@@ -235,6 +238,6 @@ fi
passwd --delete "${container_user_name}"
passwd --delete root
echo "container_setup_done"
printf "container_setup_done\n"
# Keepalive loop
sleep infinity
+11 -10
View File
@@ -10,14 +10,15 @@ verbose=0
# Outputs:
# print usage with examples.
show_help() {
echo "USAGE:
install -p /usr/local/bin
cat <<EOF
install -p /usr/local/bin
Arguments:
--path/-p: path where to deploy the files (default /usr/local/bin)
--help/-h: show this message
-v: show more verbosity
"
Options:
--path/-p: path where to deploy the files (default /usr/local/bin)
--help/-h: show this message
-v: show more verbosity
"
EOF
}
# Parse arguments
@@ -53,8 +54,8 @@ fi
# Ensure the foundamental variables are set and not empty, we will not proceed if
# they are not all set.
[ ! -d "${dest_path}" ] && echo "Path ${dest_path} does not exist." && exit 1
[ ! -w "${dest_path}" ] && echo "Cannot write into ${dest_path}, permission denied." && exit 1
[ ! -d "${dest_path}" ] && printf >&2 "Path %s does not exist.\n" "${dest_path} " && exit 1
[ ! -w "${dest_path}" ] && printf >&2 "Cannot write into %s, permission denied.\n" "${dest_path}" && exit 1
# get current dir
curr_dir=$(dirname "$0")
@@ -75,7 +76,7 @@ if [ -f "${curr_dir}/distrobox-enter" ]; then
else
# check that we have base dependencies
if ! command -v curl || ! command -v tar; then
echo "Online install depends on curl and tar"
printf >&2 "Online install depends on curl and tar\n"
exit 1
fi