Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 19, 2025
1 parent 3b0bf3b commit 814813a
Showing 1 changed file with 105 additions and 105 deletions.
210 changes: 105 additions & 105 deletions scripts/get-pants.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,141 +10,141 @@ COLOR_YELLOW="\x1b[33m"
COLOR_RESET="\x1b[0m"

function log() {
echo -e "$@" 1>&2
echo -e "$@" 1>&2
}

function die() {
(($# > 0)) && log "${COLOR_RED}$*${COLOR_RESET}"
exit 1
(($# > 0)) && log "${COLOR_RED}$*${COLOR_RESET}"
exit 1
}

function green() {
(($# > 0)) && log "${COLOR_GREEN}$*${COLOR_RESET}"
(($# > 0)) && log "${COLOR_GREEN}$*${COLOR_RESET}"
}

function warn() {
(($# > 0)) && log "${COLOR_YELLOW}$*${COLOR_RESET}"
(($# > 0)) && log "${COLOR_YELLOW}$*${COLOR_RESET}"
}

function check_cmd() {
local cmd="$1"
command -v "$cmd" > /dev/null || die "This script requires the ${cmd} binary to be on the PATH."
local cmd="$1"
command -v "$cmd" >/dev/null || die "This script requires the ${cmd} binary to be on the PATH."
}

help_url="https://www.pantsbuild.org/docs/getting-help"

_GC=()

function gc() {
if (($# > 0)); then
check_cmd rm
_GC+=("$@")
else
rm -rf "${_GC[@]}"
fi
if (($# > 0)); then
check_cmd rm
_GC+=("$@")
else
rm -rf "${_GC[@]}"
fi
}

trap gc EXIT

check_cmd uname

function calculate_os() {
local os

os="$(uname -s)"
if [[ "${os}" =~ [Ll]inux ]]; then
echo linux
elif [[ "${os}" =~ [Dd]arwin ]]; then
echo macos
elif [[ "${os}" =~ [Ww]in|[Mm][Ii][Nn][Gg] ]]; then
# Powershell reports something like: Windows_NT
# Git bash reports something like: MINGW64_NT-10.0-22621
echo windows
else
die "Pants is not supported on this operating system (${os}). Please reach out to us at ${help_url} for help."
fi
local os

os="$(uname -s)"
if [[ ${os} =~ [Ll]inux ]]; then
echo linux
elif [[ ${os} =~ [Dd]arwin ]]; then
echo macos
elif [[ ${os} =~ [Ww]in|[Mm][Ii][Nn][Gg] ]]; then
# Powershell reports something like: Windows_NT
# Git bash reports something like: MINGW64_NT-10.0-22621
echo windows
else
die "Pants is not supported on this operating system (${os}). Please reach out to us at ${help_url} for help."
fi
}

OS="$(calculate_os)"

check_cmd basename
if [[ "${OS}" == "windows" ]]; then
check_cmd pwsh
if [[ ${OS} == "windows" ]]; then
check_cmd pwsh
else
check_cmd curl
check_cmd curl
fi

function fetch() {
local url="$1"
local dest_dir="$2"
local url="$1"
local dest_dir="$2"

local dest
dest="${dest_dir}/$(basename "${url}")"
local dest
dest="${dest_dir}/$(basename "${url}")"

if [[ "${OS}" == "windows" ]]; then
pwsh -c "Invoke-WebRequest -OutFile $dest -Uri $url"
else
curl --proto '=https' --tlsv1.2 -sSfL -o "${dest}" "${url}"
fi
if [[ ${OS} == "windows" ]]; then
pwsh -c "Invoke-WebRequest -OutFile $dest -Uri $url"
else
curl --proto '=https' --tlsv1.2 -sSfL -o "${dest}" "${url}"
fi
}

if [[ "${OS}" == "macos" ]]; then
check_cmd shasum
if [[ ${OS} == "macos" ]]; then
check_cmd shasum
else
check_cmd sha256sum
check_cmd sha256sum
fi

function sha256() {
if [[ "${OS}" == "macos" ]]; then
shasum --algorithm 256 "$@"
else
sha256sum "$@"
fi
if [[ ${OS} == "macos" ]]; then
shasum --algorithm 256 "$@"
else
sha256sum "$@"
fi
}

check_cmd mktemp

function install_from_url() {
local url="$1"
local dest="$2"

local workdir
workdir="$(mktemp -d)"
gc "${workdir}"

fetch "${url}.sha256" "${workdir}"
fetch "${url}" "${workdir}"
(
cd "${workdir}"
sha256 -c --status ./*.sha256 ||
die "Download from ${url} did not match the fingerprint at ${url}.sha256"
)
rm "${workdir}/"*.sha256
if [[ "${OS}" == "macos" ]]; then
mkdir -p "$(dirname "${dest}")"
install -m 755 "${workdir}/"* "${dest}"
else
install -D -m 755 "${workdir}/"* "${dest}"
fi
local url="$1"
local dest="$2"

local workdir
workdir="$(mktemp -d)"
gc "${workdir}"

fetch "${url}.sha256" "${workdir}"
fetch "${url}" "${workdir}"
(
cd "${workdir}"
sha256 -c --status ./*.sha256 ||
die "Download from ${url} did not match the fingerprint at ${url}.sha256"
)
rm "${workdir}/"*.sha256
if [[ ${OS} == "macos" ]]; then
mkdir -p "$(dirname "${dest}")"
install -m 755 "${workdir}/"* "${dest}"
else
install -D -m 755 "${workdir}/"* "${dest}"
fi
}

function calculate_arch() {
local arch

arch="$(uname -m)"
if [[ "${arch}" =~ x86[_-]64 ]]; then
echo x86_64
elif [[ "${arch}" =~ arm64|aarch64 ]]; then
echo aarch64
else
die "Pants is not supported for this chip architecture (${arch}). Please reach out to us at ${help_url} for help."
fi
local arch

arch="$(uname -m)"
if [[ ${arch} =~ x86[_-]64 ]]; then
echo x86_64
elif [[ ${arch} =~ arm64|aarch64 ]]; then
echo aarch64
else
die "Pants is not supported for this chip architecture (${arch}). Please reach out to us at ${help_url} for help."
fi
}

check_cmd cat

function usage() {
cat << EOF
cat <<EOF
Usage: $0
Installs the pants launcher binary.
You only need to run this once on a machine when you do not have "pants"
Expand All @@ -171,29 +171,29 @@ bin_dir="${HOME}/.local/bin"
base_name="pants"
version="latest/download"
while (($# > 0)); do
case "$1" in
--help | -h)
usage
exit 0
;;
--bin-dir | -d)
bin_dir="$2"
shift
;;
--base-name | -b)
base_name="$2"
shift
;;
--version | -V)
version="download/v$2"
shift
;;
*)
usage
die "Unexpected argument $1\n"
;;
esac
shift
case "$1" in
--help | -h)
usage
exit 0
;;
--bin-dir | -d)
bin_dir="$2"
shift
;;
--base-name | -b)
base_name="$2"
shift
;;
--version | -V)
version="download/v$2"
shift
;;
*)
usage
die "Unexpected argument $1\n"
;;
esac
shift
done

ARCH="$(calculate_arch)"
Expand All @@ -203,9 +203,9 @@ dest="${bin_dir}/${base_name}"
log "Downloading and installing the pants launcher ..."
install_from_url "${URL}" "${dest}"
green "Installed the pants launcher from ${URL} to ${dest}"
if ! command -v "${base_name}" > /dev/null; then
warn "${dest} is not on the PATH."
log "You'll either need to invoke ${dest} explicitly or else add ${bin_dir} to your shell's PATH."
if ! command -v "${base_name}" >/dev/null; then
warn "${dest} is not on the PATH."
log "You'll either need to invoke ${dest} explicitly or else add ${bin_dir} to your shell's PATH."
fi

green "\nRunning \`pants\` in a Pants-enabled repo will use the version of Pants configured for that repo."
Expand Down

0 comments on commit 814813a

Please sign in to comment.