Skip to content
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

Fix Shellcheck violations #443

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions share/ruby-install/checksums.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function lookup_checksum()
return 1
fi

local output="$(grep " $file" "$checksums")"
local output
output="$(grep " $file" "$checksums")"

echo -n "${output%% *}"
}
Expand All @@ -55,7 +56,8 @@ function compute_checksum()
fi

debug "$program $file"
local output="$($program "$file")"
local output
output="$($program "$file")"

echo -n "${output%% *}"
}
Expand All @@ -71,7 +73,8 @@ function verify_checksum()
return
fi

local actual_checksum="$(compute_checksum "$algorithm" "$file")"
local actual_checksum
actual_checksum="$(compute_checksum "$algorithm" "$file")"

if [[ "$actual_checksum" != "$expected_checksum" ]]; then
error "Invalid $algorithm checksum for $file"
Expand Down
5 changes: 4 additions & 1 deletion share/ruby-install/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ function load_dependencies_from()
{
local file="$1"

ruby_dependencies=($(fetch "$ruby/$file" "$package_manager" || return $?))
ruby_dependencies=()
while IFS='' read -r line; do
ruby_dependencies+=("$line")
done < <(fetch "$ruby/$file" "$package_manager" || return $?)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually need the implicit shell-splitting here. fetch "$ruby/$file" "$package_manager" will read the dependencies.txt file and return the apt: pkg1 pkg2 ... line that matches the "$package_manager", but without the apt: prefix. It is then implicitly splatted into an Array of individual package names. Reading each line would cause ruby_dependencies to become a singleton Array that contains the whole line (ex: ("pkg1 pkg2 pkg3")).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, agree. I just followed (blindly) what Shellcheck WIKI was proposing :D

}

#
Expand Down
8 changes: 6 additions & 2 deletions share/ruby-install/package_manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function install_packages()
run $sudo pkg install -y "$@" || return $?
;;
brew)
local brew_owner="$(/usr/bin/stat -f %Su "$(command -v brew)")"
local brew_owner=
brew_owner="$(/usr/bin/stat -f %Su "$(command -v brew)")"
local brew_sudo=""

if [[ "$brew_owner" != "$(id -un)" ]]; then
Expand All @@ -40,7 +41,10 @@ function install_packages()
run $brew_sudo brew upgrade "$@" || return $?
;;
pacman)
local missing_pkgs=($(pacman -T "$@"))
local missing_pkgs=()
while IFS='' read -r line; do
missing_pkgs+=("$line")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a Arch user so I can't verify this, can you double check if pacman -T outputs the missing packages one per-line or a single-line with the package names separated by spaces?
https://archlinux.org/pacman/pacman.8.html

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have Arch installed, either. I'm just maintaining Gentoo ebuild in ::guru overlay, and currently I simply disable make check :D

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I forgot I can check using the archlinux docker image.

done < <(pacman -T "$@")

if (( ${#missing_pkgs[@]} > 0 )); then
run $sudo pacman -S "${missing_pkgs[@]}" || return $?
Expand Down
5 changes: 3 additions & 2 deletions share/ruby-install/ruby-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function parse_ruby()
case "$string" in
*-[0-9]*)
ruby="${string%-[0-9]*}"
ruby_version="${string#$ruby-}"
ruby_version="${string#"$ruby"-}"
;;
[0-9]*)
ruby="ruby"
Expand Down Expand Up @@ -272,7 +272,8 @@ function init()
return 1
fi

local fully_qualified_version="$(lookup_ruby_version "$ruby" "$ruby_version")"
local fully_qualified_version
fully_qualified_version="$(lookup_ruby_version "$ruby" "$ruby_version")"

if [[ -n "$fully_qualified_version" ]]; then
ruby_version="$fully_qualified_version"
Expand Down
2 changes: 1 addition & 1 deletion share/ruby-install/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function fetch()

while IFS="" read -r line; do
if [[ "$line" == "$key:"* ]]; then
echo "${line##$key:*([[:space:]])}"
echo "${line##"$key":*([[:space:]])}"
Copy link
Owner

@postmodern postmodern Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are quotes really needed within bash variable substitutions?

var1="foo bar"
var2="foo bar baz"
echo "${var2#$var1 }"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, but shellcheck disagrees :D

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, which version of ShellCheck are you using? I'm on 0.7.2, which might be older than your version, which might explain why I'm not seeing those warnings?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mine is 0.8.0 :D

fi
done < "$file"
}
Expand Down