Skip to content

Commit

Permalink
Make the install script a little bit safer when being piped
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Mailer authored and rlegan committed May 10, 2023
1 parent 9fdccf5 commit 15acfb8
Showing 1 changed file with 64 additions and 58 deletions.
122 changes: 64 additions & 58 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,72 @@

set -o errexit

echo "Starting installation."

# GitHub's URL for the latest release, will redirect.
GITHUB_BASE_URL="https://github.com/CircleCI-Public/circleci-cli"
LATEST_URL="${GITHUB_BASE_URL}/releases/latest/"
DESTDIR="${DESTDIR:-/usr/local/bin}"

if [ -z "$VERSION" ]; then
VERSION=$(curl -sLI -o /dev/null -w '%{url_effective}' "$LATEST_URL" | cut -d "v" -f 2)
fi

echo "Installing CircleCI CLI v${VERSION}"

# Run the script in a temporary directory that we know is empty.
SCRATCH=$(mktemp -d || mktemp -d -t 'tmp')
cd "$SCRATCH"

function error {
echo "An error occured installing the tool."
echo "The contents of the directory $SCRATCH have been left in place to help to debug the issue."
}

trap error ERR

# Determine release filename.
case "$(uname)" in
Linux)
OS='linux'
;;
Darwin)
OS='darwin'
;;
*)
echo "This operating system is not supported."
exit 1
;;
esac

case "$(uname -m)" in
aarch64 | arm64)
ARCH='arm64'
;;
x86_64)
ARCH="amd64"
;;
*)
echo "This architecture is not supported."
exit 1
;;
esac

RELEASE_URL="${GITHUB_BASE_URL}/releases/download/v${VERSION}/circleci-cli_${VERSION}_${OS}_${ARCH}.tar.gz"

# Download & unpack the release tarball.
curl --ssl-reqd -sL --retry 3 "${RELEASE_URL}" | tar zx --strip 1

echo "Installing to $DESTDIR"
install circleci "$DESTDIR"

command -v circleci

# Delete the working directory when the install was successful.
rm -r "$SCRATCH"
# Use a function to ensure connection errors don't partially execute when being piped
function install {

echo "Starting installation."

# GitHub's URL for the latest release, will redirect.
GITHUB_BASE_URL="https://github.com/CircleCI-Public/circleci-cli"
LATEST_URL="${GITHUB_BASE_URL}/releases/latest/"
DESTDIR="${DESTDIR:-/usr/local/bin}"

if [ -z "$VERSION" ]; then
VERSION=$(curl -sLI -o /dev/null -w '%{url_effective}' "$LATEST_URL" | cut -d "v" -f 2)
fi

echo "Installing CircleCI CLI v${VERSION}"

# Run the script in a temporary directory that we know is empty.
SCRATCH=$(mktemp -d || mktemp -d -t 'tmp')
cd "$SCRATCH"

trap error ERR

# Determine release filename.
case "$(uname)" in
Linux)
OS='linux'
;;
Darwin)
OS='darwin'
;;
*)
echo "This operating system is not supported."
exit 1
;;
esac

case "$(uname -m)" in
aarch64 | arm64)
ARCH='arm64'
;;
x86_64)
ARCH="amd64"
;;
*)
echo "This architecture is not supported."
exit 1
;;
esac

RELEASE_URL="${GITHUB_BASE_URL}/releases/download/v${VERSION}/circleci-cli_${VERSION}_${OS}_${ARCH}.tar.gz"

# Download & unpack the release tarball.
curl --ssl-reqd -sL --retry 3 "${RELEASE_URL}" | tar zx --strip 1

echo "Installing to $DESTDIR"
install circleci "$DESTDIR"

command -v circleci

# Delete the working directory when the install was successful.
rm -r "$SCRATCH"
}

install

0 comments on commit 15acfb8

Please sign in to comment.