Skip to content

Commit 81044c3

Browse files
refactor(readability): avoid readonly assignments via conditional chaining (#1643)
1 parent 08ff2ad commit 81044c3

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

nodebuilder

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ bitcoin_tarball_download_validate()
240240
*)
241241
BITCOIN_SOURCE="https://bitcoincore.org/bin/bitcoin-core-${target_bitcoin_version}"
242242
;;
243-
esac && readonly BITCOIN_SOURCE
243+
esac
244+
readonly BITCOIN_SOURCE
244245
readonly BITCOIN_TARBALL_FILE_SOURCE="${BITCOIN_SOURCE}/${BITCOIN_TARBALL_FILENAME}"
245246
readonly BITCOIN_HASH_FILENAME='SHA256SUMS'
246247
readonly BITCOIN_HASH_FILE_SOURCE="${BITCOIN_SOURCE}/${BITCOIN_HASH_FILENAME}"
@@ -379,7 +380,8 @@ bitcoin_tarball_test()
379380
if [ "${TARGET_ARCHITECTURE}" = 'arm64' ]; then
380381
codesign -s - "${BITCOIN_INSTALL_BIN_SOURCE}"/test_bitcoin
381382
fi
382-
UNIT_TEST_RESPONSE="$("${BITCOIN_INSTALL_BIN_SOURCE}"/test_bitcoin 2>&1)" && readonly UNIT_TEST_RESPONSE
383+
UNIT_TEST_RESPONSE="$("${BITCOIN_INSTALL_BIN_SOURCE}"/test_bitcoin 2>&1)"
384+
readonly UNIT_TEST_RESPONSE
383385
case "${UNIT_TEST_RESPONSE}" in
384386
*'No errors detected'*) ;;
385387
*)
@@ -393,7 +395,8 @@ bitcoin_tarball_validate()
393395
{
394396
bitcoin_tarball_validate_checksum
395397

396-
GPG_GOOD_SIGNATURE_COUNT="$(bitcoin_tarball_validate_count_signatures)" && readonly GPG_GOOD_SIGNATURE_COUNT
398+
GPG_GOOD_SIGNATURE_COUNT="$(bitcoin_tarball_validate_count_signatures)"
399+
readonly GPG_GOOD_SIGNATURE_COUNT
397400
if [ "${GPG_GOOD_SIGNATURE_COUNT}" -lt "${GPG_GOOD_SIGNATURES_REQUIRED}" ]; then
398401
throw_error "INVALID SIGNATURES. The download has failed. This script cannot continue due to security concerns. Please review the temporary file ${TEMP_DIRECTORY}/${GPG_SIGNATURES_FILE}."
399402
fi
@@ -677,8 +680,10 @@ get_operating_system()
677680
printf '%s\n' "${TARGET_KERNEL}"
678681
;;
679682
*)
680-
OS_RELEASE_ID="$(grep '^ID=' /etc/os-release | cut -d= -f2)" && readonly OS_RELEASE_ID
681-
OS_RELEASE_ID_LIKE="$(grep '^ID_LIKE=' /etc/os-release | cut -d= -f2)" && readonly OS_RELEASE_ID_LIKE
683+
OS_RELEASE_ID="$(grep '^ID=' /etc/os-release | cut -d= -f2)"
684+
readonly OS_RELEASE_ID
685+
OS_RELEASE_ID_LIKE="$(grep '^ID_LIKE=' /etc/os-release | cut -d= -f2)"
686+
readonly OS_RELEASE_ID_LIKE
682687
if [ -n "${OS_RELEASE_ID}" ] || [ -n "${OS_RELEASE_ID_LIKE}" ]; then
683688
printf '%s\n' "${OS_RELEASE_ID_LIKE:-${OS_RELEASE_ID}}"
684689
else
@@ -801,7 +806,8 @@ install_build_dependencies_darwin()
801806
install_build_dependencies_dnf()
802807
{
803808
readonly BUILD_DEPENDENCIES_URL="${DEPENDENCIES_BASE_URL}/build_dependencies_dnf.txt"
804-
OS_MAJOR_VERSION_ID="$(grep "^VERSION_ID=" /etc/os-release | cut -d= -f2 | tr -d '"' | cut -d. -f1)" && readonly OS_MAJOR_VERSION_ID
809+
OS_MAJOR_VERSION_ID="$(grep "^VERSION_ID=" /etc/os-release | cut -d= -f2 | tr -d '"' | cut -d. -f1)"
810+
readonly OS_MAJOR_VERSION_ID
805811

806812
case "$(grep "^ID=" /etc/os-release | cut -d= -f2 | tr -d '"')" in
807813
amzn)
@@ -1017,11 +1023,14 @@ install_runtime_dependencies_darwin()
10171023
log_info 'Ensuring git dependency via the Xcode Command Line Tools.'
10181024
# These steps were taken from: https github com/Homebrew/install/blob/aceed88a4a062e2b41dc40a7428c71309fce14c9/install.sh#L831
10191025
# TODO: find a way to install only the git binary, not all the Xcode tools. Unattended git installation shouldn't take 20+ lines of code.
1020-
CLT_INSTALL_IN_PROGRESS='/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress' && readonly CLT_INSTALL_IN_PROGRESS
1026+
CLT_INSTALL_IN_PROGRESS='/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress'
1027+
readonly CLT_INSTALL_IN_PROGRESS
10211028
sudo touch "${CLT_INSTALL_IN_PROGRESS}"
10221029
CLT_LABEL_COMMAND="/usr/sbin/softwareupdate -l | grep -B 1 -E 'Command Line Tools' | awk -F'*' '/^ *\\*/ {print \$2}' | \
1023-
sed -e 's/^ *Label: //' -e 's/^ *//' | sort -V | tail -n1 | tr -d '\n'" && readonly CLT_LABEL_COMMAND
1024-
CLT_LABEL="$(/bin/sh -c "${CLT_LABEL_COMMAND}")" && readonly CLT_LABEL
1030+
sed -e 's/^ *Label: //' -e 's/^ *//' | sort -V | tail -n1 | tr -d '\n'"
1031+
readonly CLT_LABEL_COMMAND
1032+
CLT_LABEL="$(/bin/sh -c "${CLT_LABEL_COMMAND}")"
1033+
readonly CLT_LABEL
10251034
if [ -n "${CLT_LABEL}" ]; then
10261035
sudo /usr/sbin/softwareupdate -i "${CLT_LABEL}" > /dev/null
10271036
else
@@ -1543,10 +1552,14 @@ readonly BYTES_TO_KIB=1024
15431552
readonly KIB_TO_MIB="${BYTES_TO_KIB}"
15441553
readonly MIB_TO_GIB="${BYTES_TO_KIB}"
15451554

1546-
TARGET_ARCHITECTURE="$(uname -m)" && readonly TARGET_ARCHITECTURE
1547-
TARGET_KERNEL="$(uname -s)" && readonly TARGET_KERNEL
1548-
TARGET_OPERATING_SYSTEM="$(get_operating_system)" && readonly TARGET_OPERATING_SYSTEM
1549-
TARGET_OPERATING_SYSTEM_RELEASE="$(uname -r)" && readonly TARGET_OPERATING_SYSTEM_RELEASE
1555+
TARGET_ARCHITECTURE="$(uname -m)"
1556+
readonly TARGET_ARCHITECTURE
1557+
TARGET_KERNEL="$(uname -s)"
1558+
readonly TARGET_KERNEL
1559+
TARGET_OPERATING_SYSTEM="$(get_operating_system)"
1560+
readonly TARGET_OPERATING_SYSTEM
1561+
TARGET_OPERATING_SYSTEM_RELEASE="$(uname -r)"
1562+
readonly TARGET_OPERATING_SYSTEM_RELEASE
15501563

15511564
readonly BITCOIN_CORE_REPO='https://github.com/bitcoin/bitcoin'
15521565
readonly NODEBUILDER_REPO='https://github.com/bitcoin-tools/nodebuilder'
@@ -1558,7 +1571,8 @@ if [ "${TARGET_KERNEL}" = 'NetBSD' ] && is_running_in_ci; then
15581571
TEMP_DIRECTORY="${HOME}/nodebuilder_temp"
15591572
else
15601573
TEMP_DIRECTORY="$(mktemp -d)"
1561-
fi && readonly TEMP_DIRECTORY
1574+
fi
1575+
readonly TEMP_DIRECTORY
15621576

15631577
case "${TARGET_KERNEL}" in
15641578
Linux | FreeBSD)
@@ -1576,7 +1590,8 @@ case "${TARGET_KERNEL}" in
15761590
*)
15771591
throw_error "${TARGET_KERNEL} is not supported. Please run on Linux or macOS."
15781592
;;
1579-
esac && readonly SYS_CORES_COUNT
1593+
esac
1594+
readonly SYS_CORES_COUNT
15801595
readonly SYS_CORES_PLUS_ONE="$((SYS_CORES_COUNT + 1))"
15811596

15821597
ensure_sudo_dependency
@@ -1705,7 +1720,8 @@ elif command -v bitcoind > /dev/null 2>&1; then
17051720
LAUNCH_BITCOIN_CORE_COMMAND="bitcoind --daemonwait --datadir=${BITCOIN_DATA_DIRECTORY}"
17061721
else
17071722
throw_error "Failed to install Bitcoin Core ${target_bitcoin_version}."
1708-
fi && readonly LAUNCH_BITCOIN_CORE_COMMAND
1723+
fi
1724+
readonly LAUNCH_BITCOIN_CORE_COMMAND
17091725

17101726
[ "${TARGET_KERNEL}" != 'Darwin' ] && create_application_shortcuts
17111727

@@ -1785,14 +1801,19 @@ fi
17851801
case "${TARGET_KERNEL}" in
17861802
Darwin | FreeBSD | NetBSD | OpenBSD) ;;
17871803
*)
1788-
PHYSICAL_MEMORY_TOTAL_IN_MIB="$(get_memory_metric_in_mib 'MemTotal')" && readonly PHYSICAL_MEMORY_TOTAL_IN_MIB
1789-
PHYSICAL_MEMORY_FREE_IN_MIB="$(get_memory_metric_in_mib 'MemAvailable')" && readonly PHYSICAL_MEMORY_FREE_IN_MIB
1790-
SWAP_MEMORY_TOTAL_IN_MIB="$(get_memory_metric_in_mib 'SwapTotal')" && readonly SWAP_MEMORY_TOTAL_IN_MIB
1791-
SWAP_MEMORY_FREE_IN_MIB="$(get_memory_metric_in_mib 'SwapFree')" && readonly SWAP_MEMORY_FREE_IN_MIB
1804+
PHYSICAL_MEMORY_TOTAL_IN_MIB="$(get_memory_metric_in_mib 'MemTotal')"
1805+
readonly PHYSICAL_MEMORY_TOTAL_IN_MIB
1806+
PHYSICAL_MEMORY_FREE_IN_MIB="$(get_memory_metric_in_mib 'MemAvailable')"
1807+
readonly PHYSICAL_MEMORY_FREE_IN_MIB
1808+
SWAP_MEMORY_TOTAL_IN_MIB="$(get_memory_metric_in_mib 'SwapTotal')"
1809+
readonly SWAP_MEMORY_TOTAL_IN_MIB
1810+
SWAP_MEMORY_FREE_IN_MIB="$(get_memory_metric_in_mib 'SwapFree')"
1811+
readonly SWAP_MEMORY_FREE_IN_MIB
17921812
readonly MEMORY_TOTAL_IN_MIB=$((PHYSICAL_MEMORY_TOTAL_IN_MIB + SWAP_MEMORY_TOTAL_IN_MIB))
17931813
readonly MEMORY_FREE_IN_MIB=$((PHYSICAL_MEMORY_FREE_IN_MIB + SWAP_MEMORY_FREE_IN_MIB))
17941814
free_memory="$(awk -v mem_free_mib="${MEMORY_FREE_IN_MIB}" -v mib_gib_factor="${MIB_TO_GIB}" 'BEGIN{printf "%.1f", mem_free_mib / mib_gib_factor}')"
1795-
TOTAL_MEMORY="$(awk -v mem_total_mib="${MEMORY_TOTAL_IN_MIB}" -v mib_gib_factor="${MIB_TO_GIB}" 'BEGIN{printf "%.1f", mem_total_mib / mib_gib_factor}')" && readonly TOTAL_MEMORY
1815+
TOTAL_MEMORY="$(awk -v mem_total_mib="${MEMORY_TOTAL_IN_MIB}" -v mib_gib_factor="${MIB_TO_GIB}" 'BEGIN{printf "%.1f", mem_total_mib / mib_gib_factor}')"
1816+
readonly TOTAL_MEMORY
17961817
log_info "Checking system memory... ${free_memory} of ${TOTAL_MEMORY} GiB free."
17971818

17981819
if [ "${MEMORY_TOTAL_IN_MIB}" -le $((1 * MIB_TO_GIB)) ] || [ "${MEMORY_FREE_IN_MIB}" -le '32' ]; then

0 commit comments

Comments
 (0)