-
Notifications
You must be signed in to change notification settings - Fork 4
fix: usable error when install-script deps are missing; prefer sha256sum #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -69,17 +69,60 @@ then | |||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # (STEP 2) checks that the needed tools are installed in the system | ||||||||||||||||||||||||||||||||||||||||||||||
| for tool in tar curl gzip shasum | ||||||||||||||||||||||||||||||||||||||||||||||
| # (STEP 2) checks that the needed tools are installed in the system. | ||||||||||||||||||||||||||||||||||||||||||||||
| # For the checksum verification we accept either `sha256sum` (preinstalled | ||||||||||||||||||||||||||||||||||||||||||||||
| # on most Linux distros via coreutils) or `shasum` (preinstalled on macOS). | ||||||||||||||||||||||||||||||||||||||||||||||
| # All missing tools are collected and reported together with install hints, | ||||||||||||||||||||||||||||||||||||||||||||||
| # so the user only has to install once and re-run. | ||||||||||||||||||||||||||||||||||||||||||||||
| missing_tools=() | ||||||||||||||||||||||||||||||||||||||||||||||
| for tool in tar curl gzip | ||||||||||||||||||||||||||||||||||||||||||||||
| do | ||||||||||||||||||||||||||||||||||||||||||||||
| found_tool=$(exists_in_path $tool) | ||||||||||||||||||||||||||||||||||||||||||||||
| if [ -z "$found_tool" ] | ||||||||||||||||||||||||||||||||||||||||||||||
| if [ -z "$(exists_in_path "$tool")" ] | ||||||||||||||||||||||||||||||||||||||||||||||
| then | ||||||||||||||||||||||||||||||||||||||||||||||
| echo "tool '$tool' not found" | ||||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| missing_tools+=("$tool") | ||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Pick a checksum command: prefer sha256sum (Linux default), fall back to shasum (macOS default). | ||||||||||||||||||||||||||||||||||||||||||||||
| checksum_cmd="" | ||||||||||||||||||||||||||||||||||||||||||||||
| if [ -n "$(exists_in_path sha256sum)" ] | ||||||||||||||||||||||||||||||||||||||||||||||
| then | ||||||||||||||||||||||||||||||||||||||||||||||
| checksum_cmd="sha256sum -c -" | ||||||||||||||||||||||||||||||||||||||||||||||
| elif [ -n "$(exists_in_path shasum)" ] | ||||||||||||||||||||||||||||||||||||||||||||||
| then | ||||||||||||||||||||||||||||||||||||||||||||||
| checksum_cmd="shasum -a 256 -c -" | ||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||
| missing_tools+=("sha256sum-or-shasum") | ||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+86
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is recommended to use a Bash array to store commands with their arguments. This avoids potential issues with word splitting and is considered a best practice for handling commands dynamically in Bash. Additionally, using
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if [ "${#missing_tools[@]}" -ne 0 ] | ||||||||||||||||||||||||||||||||||||||||||||||
| then | ||||||||||||||||||||||||||||||||||||||||||||||
| echo "Required tool(s) not found in \$PATH:" | ||||||||||||||||||||||||||||||||||||||||||||||
| for tool in "${missing_tools[@]}" | ||||||||||||||||||||||||||||||||||||||||||||||
| do | ||||||||||||||||||||||||||||||||||||||||||||||
| case "$tool" in | ||||||||||||||||||||||||||||||||||||||||||||||
| tar|curl|gzip) | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " - $tool" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " Debian/Ubuntu: sudo apt-get install -y $tool" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " RHEL/Fedora: sudo dnf install -y $tool" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " Alpine: sudo apk add $tool" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " macOS: preinstalled" | ||||||||||||||||||||||||||||||||||||||||||||||
| ;; | ||||||||||||||||||||||||||||||||||||||||||||||
| sha256sum-or-shasum) | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " - sha256sum (GNU coreutils) or shasum (Perl Digest::SHA)" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " Debian/Ubuntu: sudo apt-get install -y coreutils # provides sha256sum" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " or sudo apt-get install -y libdigest-sha-perl # provides shasum" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " RHEL/Fedora: sudo dnf install -y coreutils # provides sha256sum" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " or sudo dnf install -y perl-Digest-SHA # provides shasum" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " Alpine: sudo apk add coreutils # provides sha256sum" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " or sudo apk add perl-utils # provides shasum" | ||||||||||||||||||||||||||||||||||||||||||||||
| echo " macOS: shasum is preinstalled" | ||||||||||||||||||||||||||||||||||||||||||||||
| ;; | ||||||||||||||||||||||||||||||||||||||||||||||
| esac | ||||||||||||||||||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # (STEP 3) collects the latest version from GitHub | ||||||||||||||||||||||||||||||||||||||||||||||
| dbdeployer_version=$(curl -s $version_url | grep '"tag_name"' | sed -E 's/.*"v([^"]+)".*/\1/') | ||||||||||||||||||||||||||||||||||||||||||||||
| check_exit_code "curl -s $version_url" | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -167,8 +210,9 @@ then | |||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # (STEP 10) probes the checksum (extract the line for our file from checksums.txt) | ||||||||||||||||||||||||||||||||||||||||||||||
| grep "$filename" "$checksum_file" | shasum -a 256 -c - | ||||||||||||||||||||||||||||||||||||||||||||||
| check_exit_code "shasum -c for $filename" | ||||||||||||||||||||||||||||||||||||||||||||||
| # $checksum_cmd was selected in STEP 2 — either `sha256sum -c -` or `shasum -a 256 -c -`. | ||||||||||||||||||||||||||||||||||||||||||||||
| grep "$filename" "$checksum_file" | $checksum_cmd | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||||||||||||||
| check_exit_code "checksum verification for $filename" | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # (STEP 11) unpacks the archive | ||||||||||||||||||||||||||||||||||||||||||||||
| tar -xzf "$filename" | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The custom
exists_in_pathfunction (defined at line 49) is potentially fragile because it splits$PATHon spaces (line 51), which will fail if any directory in the path contains a space. Since this script is using Bash, it is more robust and idiomatic to use thecommand -vbuiltin to check for the existence of executables.References
command -vbuiltin instead of manual path searching to robustly check for executable existence in shell scripts.