client: lib_docker: add 'type' arg to version

- Add `version` types (client | container | short | all)
- Related refactoring
This commit is contained in:
2024-07-11 18:23:03 -07:00
parent 938d762201
commit 85a9cd8018

View File

@@ -563,94 +563,175 @@ function lib_docker::__edit()
$EDITOR "${_paths[@]}"
}
function lib_docker::__parse_args_version()
{
[ -z "$global_usage" ] && lib_utils::die_fatal
[ -z "$global_arg_delim_1" ] && lib_utils::die_fatal
[ -z "$global_arg_delim_2" ] && lib_utils::die_fatal
[ -z "$global_arg_delim_3" ] && lib_utils::die_fatal
local -r _usage="
\e[32mDescription:\e[0m
Print version information
\e[32mUsage:\e[0m
$ $global_usage type${global_arg_delim_2}<type>
\e[32mArguments:\e[0m
Version type:
type${global_arg_delim_2}<client|container|short|all>
\e[32mExamples:\e[0m
\e[37;2m# Print docker-finance client dependency versions\e[0m
$ $global_usage type${global_arg_delim_2}client
\e[37;2m# Print docker-finance container dependency versions\e[0m
$ $global_usage type${global_arg_delim_2}container
\e[37;2m# Print only docker-finance version\e[0m
$ $global_usage type${global_arg_delim_2}short
\e[37;2m# Print both docker-finance and client dependency versions\e[0m
$ $global_usage type${global_arg_delim_2}client${global_arg_delim_3}short
\e[37;2m# Print docker-finance version and all client/container dependency versions\e[0m
$ $global_usage type${global_arg_delim_2}all
"
[ $# -eq 0 ] && lib_utils::die_usage "$_usage"
for _arg in "$@"; do
[[ ! "$_arg" =~ ^type[s]?${global_arg_delim_2} ]] \
&& lib_utils::die_usage "$_usage"
done
# Parse key for value
for _arg in "$@"; do
local _key="${_arg%${global_arg_delim_2}*}"
local _len="$((${#_key} + 1))"
if [[ "$_key" =~ ^type[s]?$ ]]; then
local _arg_type="${_arg:${_len}}"
[ -z "$_arg_type" ] && lib_utils::die_usage "$_usage"
fi
done
IFS="$global_arg_delim_3"
# Arg: type
read -ra _read <<<"$_arg_type"
for _arg in "${_read[@]}"; do
if [[ ! "$_arg" =~ ^client$|^container$|^short$|^all$ ]]; then
lib_utils::die_usage "$_usage"
fi
done
if [[ "${_read[*]}" =~ all ]]; then # Note: not anchored
declare -gr global_arg_type=("client" "container" "short")
else
declare -gr global_arg_type=("${_read[@]}")
fi
}
function lib_docker::__version()
{
lib_docker::__parse_args_version "$@"
[ -z "${DOCKER_FINANCE_VERSION}" ] && lib_utils::die_fatal
[ -z "${DOCKER_FINANCE_CLIENT_REPO}" ] && lib_utils::die_fatal
#
# `docker-finance` version
#
[ -z "${global_arg_type[*]}" ] && lib_utils::die_fatal
local _hash
if pushd "$DOCKER_FINANCE_CLIENT_REPO" 1>/dev/null; then
_hash="$(git log --pretty='format:%h' -n 1)"
popd 1>/dev/null || lib_utils::die_fatal
else
lib_utils::die_fatal
fi
for _type in "${global_arg_type[@]}"; do
echo
echo -e "docker-finance v${DOCKER_FINANCE_VERSION} | commit: $_hash"
echo -e "───────────────────────────────────────"
case "$_type" in
#
# Client dependencies
#
client)
echo
echo "client.system:"
echo
echo "client.system:"
echo -e \\t"$(uname -rmo)"
echo -e \\t"$(bash --version | head -n1)"
echo -e \\t"$(sed --version | head -n1)"
echo -e \\t"$(uname -rmo)"
echo -e \\t"$(bash --version | head -n1)"
echo -e \\t"$(sed --version | head -n1)"
echo
echo "client.docker:"
echo
echo "client.docker:"
echo -e \\t"$(docker compose version)"
docker version \
| while read _line; do
echo -e \\t"${_line}"
done
;;
echo -e \\t"$(docker compose version)"
docker version \
| while read _line; do
echo -e \\t"${_line}"
done
container)
[ -z "$global_platform" ] && lib_utils::die_fatal
#
# Container dependencies
#
# Feed dependency manifest (client is not bind-mounted for security reasons)
local _manifest
_manifest="$(<${DOCKER_FINANCE_CLIENT_REPO}/client/docker-finance.yaml)"
[ -z "$global_platform" ] && lib_utils::die_fatal
# NOTE: uses variable "callback"
case "$global_platform" in
archlinux)
_platform="$global_platform"
_image="finance"
_pkg="pacman -Q \$_package"
;;
ubuntu | dev-tools)
if [[ "$global_platform" =~ ^ubuntu$|^dev-tools$ ]]; then
_platform="ubuntu"
_image="finance"
[[ "$global_platform" == "dev-tools" ]] && _image="dev-tools"
fi
_pkg="dpkg -s \$_package | grep -E \"(^Package:|^Version:)\" | cut -d' ' -f2 | paste -s | awk '{print \$1 \" \" \$2}'"
;;
*)
lib_utils::die_fatal "unsupported platform"
;;
esac
# Feed dependency manifest (client is not bind-mounted for security reasons)
local _manifest
_manifest="$(<${DOCKER_FINANCE_CLIENT_REPO}/client/docker-finance.yaml)"
lib_docker::__run "
echo '${_manifest}' \\
| shyaml keys container.${_platform}.${_image} \\
| while read _key; do
echo -e \\\ncontainer.${_platform}.${_image}.\${_key}:
echo '${_manifest}' | shyaml get-values container.${_platform}.${_image}.\${_key}.packages 2>/dev/null \\
| while read _package; do
echo -e \\\t\$($_pkg)
done
echo '${_manifest}' | shyaml get-values container.${_platform}.${_image}.\${_key}.commands 2>/dev/null \\
| while read _command; do
echo -e \\\t\$(\$_command)
done
done
echo"
;;
case "$global_platform" in
# NOTE: uses variable "callback"
archlinux)
_platform="$global_platform"
_image="finance"
_pkg="pacman -Q \$_package"
;;
ubuntu | dev-tools)
if [[ "$global_platform" =~ ^ubuntu$|^dev-tools$ ]]; then
_platform="ubuntu"
_image="finance"
[[ "$global_platform" == "dev-tools" ]] && _image="dev-tools"
fi
_pkg="dpkg -s \$_package | grep -E \"(^Package:|^Version:)\" | cut -d' ' -f2 | paste -s | awk '{print \$1 \" \" \$2}'"
;;
*)
lib_utils::die_fatal "unsupported platform"
;;
esac
short)
local _hash
if pushd "$DOCKER_FINANCE_CLIENT_REPO" 1>/dev/null; then
_hash="$(git log --pretty='format:%h' -n 1)"
popd 1>/dev/null || lib_utils::die_fatal
else
lib_utils::die_fatal
fi
lib_docker::__run "
echo '${_manifest}' \\
| shyaml keys container.${_platform}.${_image} \\
| while read _key; do
echo -e \\\ncontainer.${_platform}.${_image}.\${_key}:
echo '${_manifest}' | shyaml get-values container.${_platform}.${_image}.\${_key}.packages 2>/dev/null \\
| while read _package; do
echo -e \\\t\$($_pkg)
done
echo '${_manifest}' | shyaml get-values container.${_platform}.${_image}.\${_key}.commands 2>/dev/null \\
| while read _command; do
echo -e \\\t\$(\$_command)
done
done
echo"
echo
echo -e "docker-finance v${DOCKER_FINANCE_VERSION} | commit: $_hash"
;;
return $?
*)
lib_utils::die_fatal "unsupported type"
;;
esac
done
}
# vim: sw=2 sts=2 si ai et