Skip to content

Commit

Permalink
Simplify unset-or-null expansions for shfmt 3.6.0
Browse files Browse the repository at this point in the history
Context: mvdan/sh#849
  • Loading branch information
tomwassenberg committed Dec 12, 2022
1 parent 325fc77 commit 4499535
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
36 changes: 18 additions & 18 deletions certbot-ocsp-fetcher
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ determine_colored_output() {
readonly RED='\033[0;31m'
readonly COLOR_DEFAULT='\033[0m'

if [[ -v NO_COLOR || ${TERM:-} == dumb ]]; then
if [[ -v NO_COLOR || ${TERM-} == dumb ]]; then
COLORED_STDOUT=false COLORED_STDERR=false
else
[[ -t 1 ]] || COLORED_STDOUT=false
Expand All @@ -28,7 +28,7 @@ determine_colored_output() {
exit_with_error() {
local error_prefix=error:$'\t\t'

[[ ${COLORED_STDERR:-} != false ]] &&
[[ ${COLORED_STDERR-} != false ]] &&
local -r COLORED_ERROR_MSG=${RED}${error_prefix}${*}${COLOR_DEFAULT}

# We will have closed file descriptor 2 unless verbosity was requested, so we
Expand Down Expand Up @@ -114,7 +114,7 @@ Usage: ${0##*/} [-c/--certbot-dir DIRECTORY] [-f/--force-update] \\
if [[ ${parameter} =~ --certbot-dir=(.+) ]]; then
CERTBOT_DIR=${BASH_REMATCH[1]}
else
if [[ -n ${2:-} ]]; then
if [[ -n ${2-} ]]; then
CERTBOT_DIR=${2}
shift
else
Expand Down Expand Up @@ -184,7 +184,7 @@ EOSTRING
local cert_lineages_value=${BASH_REMATCH[1]}
shift
else
if [[ -n ${2:-} ]]; then
if [[ -n ${2-} ]]; then
local cert_lineages_value=${2}
shift 2
else
Expand All @@ -198,9 +198,9 @@ EOSTRING
declare -Ag CERT_LINEAGES
# Check if a hardcoded OCSP responder was specified for this set of
# lineages.
case ${1:-} in
case ${1-} in
-u | --ocsp-responder)
if [[ -n ${2:-} ]]; then
if [[ -n ${2-} ]]; then
for lineage_name in ${cert_lineages_value}; do
CERT_LINEAGES["${lineage_name}"]=${2}
done
Expand Down Expand Up @@ -236,7 +236,7 @@ EOSTRING
if [[ ${parameter} =~ --output-dir=(.+) ]]; then
OUTPUT_DIR=${BASH_REMATCH[1]}
else
if [[ -n ${2:-} ]]; then
if [[ -n ${2-} ]]; then
OUTPUT_DIR=${2}
shift
else
Expand Down Expand Up @@ -447,7 +447,7 @@ check_for_existing_ocsp_staple_file() {
local -r next_update=${BASH_REMATCH[1]}
fi
done
[[ -n ${this_update:-} && -n ${next_update:-} ]] || return 1
[[ -n ${this_update-} && -n ${next_update-} ]] || return 1

# Only continue fetching OCSP response if existing response expires within
# half of its lifetime.
Expand Down Expand Up @@ -494,7 +494,7 @@ fetch_ocsp_response() {
# `set -o errexit` is not respected here, but in case of failure we still
# err on the safe side by renewing the OCSP staple file.
# shellcheck disable=2310
if [[ ${FORCE_UPDATE:-} != true ]] &&
if [[ ${FORCE_UPDATE-} != true ]] &&
check_for_existing_ocsp_staple_file; then
lineages_processed["${lineage_name}"]="not updated"$'\t'"valid staple file on disk"
return
Expand Down Expand Up @@ -581,7 +581,7 @@ print_and_handle_result() {
local lineages_processed_marked_up
for lineage_name in "${!lineages_processed[@]}"; do
lineages_processed_marked_up+=$'\n'"${lineage_name}"$'\t'
if [[ ${COLORED_STDOUT:-} != false ]]; then
if [[ ${COLORED_STDOUT-} != false ]]; then
if [[ ${lineages_processed["${lineage_name}"]} =~ ^updated ]]; then
lineages_processed_marked_up+=${GREEN}
elif [[ ${lineages_processed["${lineage_name}"]} =~ ^"failed to update" ]]; then
Expand All @@ -593,14 +593,14 @@ print_and_handle_result() {
fi
done
unset lineage_name
lineages_processed_marked_up=$(sort <<<"${lineages_processed_marked_up:-}")
lineages_processed_marked_up=$(sort <<<"${lineages_processed_marked_up-}")
readonly lineages_processed_marked_up

if [[ ${RELOAD_WEBSERVER:-} != false ]]; then
if [[ ${RELOAD_WEBSERVER-} != false ]]; then
reload_webserver
fi

local output=${header}${lineages_processed_marked_up:-}${nginx_status-}
local output=${header}${lineages_processed_marked_up-}${nginx_status-}

if ((VERBOSITY >= 1)); then
local output_table
Expand All @@ -626,31 +626,31 @@ print_and_handle_result() {
# Remove header before printing everything else to stdout
[[ -n ${!lineages_processed[*]} ]] && printf '%b\n' "${output#*$'\n'}"

if [[ ${COLORED_STDERR:-} != false ]]; then
if [[ ${COLORED_STDERR-} != false ]]; then
printf %b "${RED}${column_error[*]-}${COLOR_DEFAULT}" >&2
else
printf %b "${column_error[*]-}" >&2
fi
fi

[[ ${ERROR_ENCOUNTERED:-} != true ]]
[[ ${ERROR_ENCOUNTERED-} != true ]]
}

reload_webserver() {
for lineage_name in "${!lineages_processed[@]}"; do
if [[ ${lineages_processed["${lineage_name}"]} == updated ]]; then
local nginx_status
if nginx -s reload >&3 2>&1; then
[[ ${COLORED_STDERR:-} != false ]] && nginx_status=${GREEN}
[[ ${COLORED_STDERR-} != false ]] && nginx_status=${GREEN}
# The last line includes a leading space, to workaround the lack of the
# `-n` flag in later versions of `column`.
nginx_status+=$'\n\n \t'"nginx reloaded"
else
ERROR_ENCOUNTERED=true
[[ ${COLORED_STDERR:-} != false ]] && nginx_status=${RED}
[[ ${COLORED_STDERR-} != false ]] && nginx_status=${RED}
nginx_status=$'\n\n \t'"nginx not reloaded"$'\t'"unable to reload nginx service, try manually"
fi
[[ ${COLORED_STDERR:-} != false ]] &&
[[ ${COLORED_STDERR-} != false ]] &&
readonly nginx_status+=${COLOR_DEFAULT}
break
fi
Expand Down
14 changes: 7 additions & 7 deletions tests/_test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ setup() {
--output-dir "${OUTPUT_DIR}"
)

if [[ ${CI:-} == true ]]; then
if [[ ${CI-} == true ]]; then
ln --symbolic "${CERTBOT_ACCOUNTS_DIR:?}/" "${CERTBOT_CONFIG_DIR}"

readonly TOOL_COMMAND_LINE
Expand All @@ -55,23 +55,23 @@ fetch_sample_certs() {
while ((${#} > 0)); do
case ${1} in
valid-example)
if [[ ${CI:-} == true ]]; then
if [[ ${CI-} == true ]]; then
lineages_host["${1}"]="${UNIQUE_TEST_PREFIX}.${CERT_DOMAIN_FOR_CI:?}"
else
lineages_host["${1}"]="valid-isrgrootx2.letsencrypt.org"
fi
shift
;;
expired-example)
if [[ ${CI:-} == true ]]; then
if [[ ${CI-} == true ]]; then
lineages_host["${1}"]="${UNIQUE_TEST_PREFIX}.${CERT_DOMAIN_FOR_CI:?}"
else
lineages_host["${1}"]="expired-isrgrootx2.letsencrypt.org"
fi
shift
;;
revoked-example)
if [[ ${CI:-} == true ]]; then
if [[ ${CI-} == true ]]; then
lineages_host["${1}"]="${UNIQUE_TEST_PREFIX}.${CERT_DOMAIN_FOR_CI:?}"
else
lineages_host["${1}"]="revoked-isrgrootx2.letsencrypt.org"
Expand All @@ -89,7 +89,7 @@ fetch_sample_certs() {
done

for lineage_name in "${!lineages_host[@]}"; do
if [[ ${CI:-} == true ]]; then
if [[ ${CI-} == true ]]; then
certbot \
--config-dir "${CERTBOT_CONFIG_DIR}" \
--logs-dir "${CERTBOT_LOGS_DIR}" \
Expand Down Expand Up @@ -136,7 +136,7 @@ fetch_sample_certs() {
fi
done

if [[ ${multiple:-} == true ]]; then
if [[ ${multiple-} == true ]]; then
mv \
"${CERTBOT_CONFIG_DIR}/live/valid-example/" \
"${CERTBOT_CONFIG_DIR}/live/valid-example_1"
Expand All @@ -156,7 +156,7 @@ teardown() {
# Revoke all certs that were issued for these tests and not revoked yet, so
# it's clear they were ephemeral and don't trigger notifications at their
# expiry.
if [[ ${CI:-} == true ]]; then
if [[ ${CI-} == true ]]; then
for lineage_path in \
"${CERTBOT_CONFIG_DIR}"/live/{expired,valid}-example{,_*}/; do
certbot \
Expand Down
2 changes: 1 addition & 1 deletion tests/fail_when_fetching_for_expired_cert.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load _test_helper
--certbot-dir "${CERTBOT_CONFIG_DIR}"
)

if [[ ${CI:-} == true ]]; then
if [[ ${CI-} == true ]]; then
fetch_sample_certs expired-example

# Use `faketime` to trick the tool into thinking this is an expired
Expand Down
2 changes: 1 addition & 1 deletion tests/fail_when_fetching_for_revoked_cert.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load _test_helper
@test "fail when fetching OCSP response for revoked certificate" {
fetch_sample_certs revoked-example valid-example

if [[ ${CI:-} == true ]]; then
if [[ ${CI-} == true ]]; then
certbot \
--config-dir "${CERTBOT_CONFIG_DIR}" \
--logs-dir "${CERTBOT_LOGS_DIR}" \
Expand Down
2 changes: 1 addition & 1 deletion tests/fetch_responses_from_supplied_ocsp_responder.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load _test_helper
@test "fetch OCSP responses from supplied OCSP responder" {
fetch_sample_certs --multiple valid-example

if [[ ${CI:-} == true ]]; then
if [[ ${CI-} == true ]]; then
local ocsp_responder=http://stg-e1.o.lencr.org
else
local ocsp_responder=http://e1.o.lencr.org
Expand Down

0 comments on commit 4499535

Please sign in to comment.