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

Make the install script a little bit safer when being piped #352

Merged
merged 1 commit into from
May 10, 2023
Merged
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
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