fix: avoid echo usage

Echo can interpret operand as an option and checking every variable to
be echoed is troublesome while with printf, if the format specifier is
present before the operand, printing as string can be enforced.
This commit is contained in:
Ben Grande 2024-08-06 18:15:24 +02:00
parent 1b2f1ba941
commit bdd4c789c1
No known key found for this signature in database
GPG key ID: 00C64E14F51F9E56
52 changed files with 318 additions and 270 deletions

View file

@ -10,11 +10,11 @@ set -eu
usage(){
names="$(find salt/ -mindepth 1 -maxdepth 1 -type d -printf '%f\n' \
| sort -d | tr "\n" " ")"
keys_trimmed="$(echo "${keys}" | tr "\n" " ")"
echo "Usage: ${0##*/} <NAME> <KEY>"
echo "Example: ${0##*/} qubes-builder description"
echo "Names: ${names}"
echo "Keys: ${keys_trimmed}"
keys_trimmed="$(printf '%s\n' "${keys}" | tr "\n" " ")"
printf '%s\n' "Usage: ${0##*/} <NAME> <KEY>"
printf '%s\n' "Example: ${0##*/} qubes-builder description"
printf '%s\n' "Names: ${names}"
printf '%s\n' "Keys: ${keys_trimmed}"
}
block_max_chars(){
@ -22,8 +22,9 @@ block_max_chars(){
char_value="${2}"
less_than="${3}"
if test "${#char_value}" -ge "${less_than}"; then
echo "Error: ${char_key} is too long. Must be <${less_than} chars." >&2
echo "Key contents: ${char_value}" >&2
err_msg="Error: ${char_key} is too long. Must be <${less_than} chars."
printf '%s\n' "${err_msg}" >&2
printf '%s\n' "Key contents: ${char_value}" >&2
exit 1
fi
}
@ -60,11 +61,12 @@ case "${1-}" in
*) key="${1}"; shift;;
esac
if test -z "${key##* }"; then
echo "Key was not given" >&2
printf '%s\n' "Key was not given" >&2
exit 1
fi
command -v git >/dev/null || { echo "Missing program: git" >&2; exit 1; }
command -v git >/dev/null ||
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
repo_toplevel="$(git rev-parse --show-toplevel)"
test -d "${repo_toplevel}" || exit 1
cd "${repo_toplevel}"
@ -87,7 +89,8 @@ bug_url="${SPEC_BUGURL:-"${url}/issues"}"
if test -z "${group}" || test -z "${vendor}" || test -z "${packager}" \
|| test -z "${url}" || test -z "${bug_url}"
then
echo "At least one empty var: group, vendor, packager, url, bug_url" >&2
err_msg="At least one empty var: group, vendor, packager, url, bug_url"
printf '%s\n' "${err_msg}" >&2
exit 1
fi
@ -96,7 +99,7 @@ project="${group}-${name}"
project_dir="salt/${name}"
if ! test -d "${project_dir}"; then
echo "Project doesn't exist: ${project_dir}" >&2
printf '%s\n' "Project doesn't exist: ${project_dir}" >&2
exit 1
fi
@ -104,7 +107,7 @@ fi
read -r version <"${project_dir}/version"
readme="${project_dir}/README.md"
if ! test -f "${readme}"; then
echo "Project ${name} does not have README.md" >&2
printf '%s\n' "Project ${name} does not have README.md" >&2
exit 1
fi
@ -112,7 +115,7 @@ if test "${key}" = "license" || test "${key}" = "license_csv"; then
license_csv="$(reuse --root "${project_dir}" lint |
awk -F ':' '/^\* Used licenses:/{print $2}' | tr " " "\n" | tr -d "," |
sort -d | tr -s "\n" "," | sed "s/^\,//;s/\,$//")"
license="$(echo "${license_csv}" | sed "s/\,/ AND /g")"
license="$(printf '%s\n' "${license_csv}" | sed "s/\,/ AND /g")"
fi
## The macro %autochangelog prints logs of all projects and we separate a
@ -167,25 +170,25 @@ if test "${key}" = "saltfiles" || test "${key}" = "requires"; then
fi
case "${key}" in
branch) echo "${branch}";;
changelog) echo "${changelog}";;
description) echo "${description}";;
file_roots) echo "${file_roots}";;
group) echo "${group}";;
license_csv) echo "${license_csv}";;
license) echo "${license}";;
name) echo "${name}";;
project) echo "${project}";;
project_dir) echo "${project_dir}";;
readme) echo "${readme}";;
requires) echo "${requires}";;
saltfiles) echo "${saltfiles}";;
summary) echo "${summary}";;
url) echo "${url}";;
bug_url) echo "${bug_url}";;
vendor) echo "${vendor}";;
packager) echo "${packager}";;
version) echo "${version}";;
branch) printf '%s\n' "${branch}";;
changelog) printf '%s\n' "${changelog}";;
description) printf '%s\n' "${description}";;
file_roots) printf '%s\n' "${file_roots}";;
group) printf '%s\n' "${group}";;
license_csv) printf '%s\n' "${license_csv}";;
license) printf '%s\n' "${license}";;
name) printf '%s\n' "${name}";;
project) printf '%s\n' "${project}";;
project_dir) printf '%s\n' "${project_dir}";;
readme) printf '%s\n' "${readme}";;
requires) printf '%s\n' "${requires}";;
saltfiles) printf '%s\n' "${saltfiles}";;
summary) printf '%s\n' "${summary}";;
url) printf '%s\n' "${url}";;
bug_url) printf '%s\n' "${bug_url}";;
vendor) printf '%s\n' "${vendor}";;
packager) printf '%s\n' "${packager}";;
version) printf '%s\n' "${version}";;
"") exit 1;;
*) echo "Unsupported key" >&2; exit 1;;
*) printf '%s\n' "Unsupported key" >&2; exit 1;;
esac