Skip to content

Commit

Permalink
Use type instead of command -v in scripts
Browse files Browse the repository at this point in the history
This commit will update `bootstrap.sh` and `linuxbrew.sh` to detect
executable programs with `type` instead of with `command -v`.
The `command` executable does not exist on Linux GitHub Actions runners.
This is strange because `command` is included in the POSIX spec.
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/command.html

To avoid errors, the `type` executable will be used instead. `type -P`
can output the path to the executable, but the `-P` option is specific
to Bash and is not a POSIX default. For portability, `type` will be used
without options where possible.
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/type.html
  • Loading branch information
br3ndonland committed Nov 14, 2024
1 parent 227df23 commit 6dcc778
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
echo "/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin" >>"$GITHUB_PATH"
- name: Clean up Homebrew
run: |
command -v brew &>/dev/null && brew test-bot --only-cleanup-before ||
type brew &>/dev/null && brew test-bot --only-cleanup-before ||
echo "Homebrew not found."
- name: Clean up macOS
if: runner.os == 'macOS'
Expand Down
8 changes: 4 additions & 4 deletions .zshrc
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ if [[ -d $HOMEBREW_PREFIX ]]; then
fi

### exports
if command -v codium &>/dev/null; then
if type codium &>/dev/null; then
editor='codium --wait'
elif command -v code &>/dev/null; then
elif type code &>/dev/null; then
editor='code --wait'
elif command -v code-insiders &>/dev/null; then
elif type code-insiders &>/dev/null; then
editor='code-insiders --wait'
elif command -v code-exploration &>/dev/null; then
elif type code-exploration &>/dev/null; then
editor='code-exploration --wait'
else
editor='vim'
Expand Down
4 changes: 2 additions & 2 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,10 @@ set_up_brew_skips() {

run_brew_installs() {
local brewfile_domain brewfile_path brewfile_url git_branch github_user
if ! command -v brew &>/dev/null; then
if ! type brew &>/dev/null; then
log "brew command not in shell environment. Attempting to load."
eval "$("$HOMEBREW_PREFIX"/bin/brew shellenv)"
command -v brew &>/dev/null && logk || return 1
type brew &>/dev/null && logk || return 1
fi
# Disable Homebrew Analytics: https://docs.brew.sh/Analytics
brew analytics off
Expand Down
22 changes: 8 additions & 14 deletions scripts/linuxbrew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -eo pipefail

# Set up DEB packages
# Proton VPN: https://protonvpn.com/support/linux-vpn-tool/
if command -v xdg-user-dir &>/dev/null; then
if type xdg-user-dir &>/dev/null; then
download_dir=$(xdg-user-dir DOWNLOAD)
else
mkdir -p "$HOME/Downloads"
Expand Down Expand Up @@ -51,19 +51,13 @@ detect_homebrew_prefix() {
}

# Download and install Homebrew: https://docs.brew.sh/Homebrew-on-Linux
if command -v brew &>/dev/null; then
printf "\nHomebrew detected.\n"
else
printf "\nbrew command not in shell environment. Attempting to load."
detect_homebrew_prefix
eval "$("$HOMEBREW_PREFIX"/bin/brew shellenv)"
if ! command -v brew &>/dev/null; then
printf "\nHomebrew not found. Downloading and installing Homebrew.\n"
BREW_SCRIPT="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
NONINTERACTIVE=1 /usr/bin/env bash -c "$(curl -fsSL $BREW_SCRIPT)"
fi
if ! type brew &>/dev/null; then
printf "\nHomebrew not found. Downloading and installing Homebrew.\n"
BREW_SCRIPT="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
NONINTERACTIVE=${STRAP_CI:-1} /usr/bin/env bash -c "$(curl -fsSL $BREW_SCRIPT)"
detect_homebrew_prefix
eval "$("$HOMEBREW_PREFIX"/bin/brew shellenv)"
command -v brew &>/dev/null || printf "\nError: Homebrew not found" && exit 1
type brew
printf "\nAdding Homebrew to ~/.profile\n"
printf %s "eval \"$("$HOMEBREW_PREFIX"/bin/brew shellenv)\"" >>~/.profile
fi
echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >>~/.profile
8 changes: 4 additions & 4 deletions scripts/python-user-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ pipx_install_requirements() {
elif [[ $(echo "$pipx_list" | grep -ce "package $package") -gt 0 ]]; then
echo "Reinstalling $package for $py."
pipx uninstall "$package"
pipx install --python "$(command -v python3)" "$package"
elif command -v "$package_command" &>/dev/null; then
echo "$package already on PATH at $(command -v "$package_command")."
pipx install --python "$py" "$package"
elif type "$package_command"; then
echo "$package already on \$PATH."
else
pipx install "$package"
fi
done <"$1"
}

if ! command -v pipx &>/dev/null; then
if ! type pipx &>/dev/null; then
printf "\n-> Error: pipx must be installed and on PATH.\n\n"
exit 1
elif pipx_install_requirements "$@"; then
Expand Down
8 changes: 4 additions & 4 deletions scripts/strap-after-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ else
fi

### Install Hatch
if command -v pipx &>/dev/null && ! command -v hatch &>/dev/null; then
if type pipx &>/dev/null && ! type hatch &>/dev/null; then
echo "Installing Hatch with pipx."
pipx install "hatch>=1,<2"
else
Expand All @@ -30,11 +30,11 @@ for i in {code,code-exploration,code-insiders,code-server,codium}; do
done

### Set shell
if ! [[ $SHELL =~ "zsh" ]] && command -v zsh &>/dev/null; then
if ! [[ $SHELL =~ "zsh" ]] && type zsh &>/dev/null; then
echo "--> Changing shell to Zsh. Password entry required."
[ "${LINUX:-0}" -gt 0 ] || [ "$(uname)" = "Linux" ] &&
command -v zsh | sudo tee -a /etc/shells
sudo chsh -s "$(command -v zsh)" "$USER"
type -P zsh | sudo tee -a /etc/shells
sudo chsh -s "$(type -P zsh)" "$USER"
else
echo "Shell is already set to Zsh."
fi
12 changes: 6 additions & 6 deletions scripts/vscode-extensions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# CLI: https://code.visualstudio.com/docs/editor/extension-marketplace

check_open_vsx() {
if ! command -v curl &>/dev/null || ! (
command -v fx || command -v jq
if ! type curl &>/dev/null || ! (
type fx || type jq
) &>/dev/null; then
printf "curl and jq or antonmedv/fx required to check extension version.\n"
return
Expand All @@ -14,9 +14,9 @@ check_open_vsx() {
local_version=$(printf %s "$2" | cut -d @ -f 2)
url="https://open-vsx.org/api/$(echo "$extension_name" | tr '.' '/')"
response=$(curl -fs "$url" -H "accept: application/json")
if command -v fx &>/dev/null; then
if type fx &>/dev/null; then
open_vsx_version=$(printf %s "$response" | fx .version)
elif command -v jq &>/dev/null; then
elif type jq &>/dev/null; then
open_vsx_version=$(printf %s "$response" | jq -r .version)
fi
if [ "$local_version" = "$open_vsx_version" ]; then
Expand Down Expand Up @@ -70,13 +70,13 @@ for i in "$@"; do
codium) : "VSCodium" ;;
esac
MACOS_BIN="/Applications/$_.app/Contents/Resources/app/bin"
if command -v "$i" &>/dev/null; then
if type "$i" &>/dev/null; then
printf "\n%s command on PATH.\n" "$i"
elif [ "$(uname -s)" = "Darwin" ] && [ -d "$MACOS_BIN" ]; then
export PATH="$MACOS_BIN:$PATH"
printf "\n%s command loaded onto PATH.\n" "$i"
fi
if ! command -v "$i" &>/dev/null; then
if ! type "$i" &>/dev/null; then
printf "\nError: %s command not on PATH.\n" "$i" >&2
exit 1
elif install_extensions "$i"; then
Expand Down

0 comments on commit 6dcc778

Please sign in to comment.