Skip to content

Commit

Permalink
Update buf installer script
Browse files Browse the repository at this point in the history
  • Loading branch information
havan committed Dec 11, 2023
1 parent db693dd commit 070d086
Showing 1 changed file with 108 additions and 34 deletions.
142 changes: 108 additions & 34 deletions scripts/buf-installer.sh
Original file line number Diff line number Diff line change
@@ -1,53 +1,127 @@
#!/bin/bash

BIN="/usr/local/bin"
GITHUB_API_URL="https://api.github.com/repos/bufbuild/buf/releases/latest"
GITHUB_RELEASES_URL="https://api.github.com/repos/bufbuild/buf/releases"
# Functions
show_help() {
echo "This script downloads and installs the buf CLI utility."
echo "Usage: $0 [OPTION]"
echo ""
echo "Options:"
echo " --version=VERSION Install a specific version of buf, Ex: v1.28.1"
echo " --do-not-validate Do not validate version from releases"
echo " --list List available versions without installing"
echo " --help Display this help message"
}

validate_dependencies() {
for dep in curl jq; do
command -v $dep >/dev/null 2>&1 || { echo >&2 "This script requires $dep but it's not installed. Aborting."; exit 1; }
done
}

get_available_versions() {
curl -s "${GITHUB_API_URL_RELEASES}" | jq -r '.[].tag_name'
}

# Function to fetch latest version
fetch_latest_version() {
curl -sSL $GITHUB_API_URL | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | sed 's/v//'
validate_version() {
local available_versions=$1
local version_regex="^$VERSION$"
if ! grep -qx "$version_regex" <<< "$available_versions"; then
echo -e "Error: Version '$VERSION' not found. \nAvailable versions: \n$available_versions" >&2
exit 1
fi
}

# Function to list all versions
list_versions() {
curl -sSL $GITHUB_RELEASES_URL | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'
local available_versions=$1
echo "Available buf versions:"
echo "$available_versions"
exit 0
}

download_and_install() {
local version=$1
local temp_file=$(mktemp)

# Download the file
local download_url="https://github.com/bufbuild/buf/releases/download/${version}/buf-$(uname -s)-$(uname -m)"
echo "Downloading: $download_url"
if ! curl -sSL "$download_url" -o "$temp_file"; then
echo "Error: Failed to download buf version ${version}."
rm -f "$temp_file"
exit 1
fi

# Move the file to the bin directory
if ! mv "$temp_file" "${BIN}/buf"; then
echo "Error: Failed to move buf to ${BIN}."
rm -f "$temp_file"
exit 1
fi

# Set executable permissions
if ! chmod +x "${BIN}/buf"; then
echo "Error: Failed to set executable permissions on buf."
exit 1
fi

echo "buf version ${version} installed successfully."
}

# Argument parsing
VERSION=""
LIST_VERSIONS=false
for i in "$@"
do
case $i in
LIST_ONLY=false
SHOW_HELP=false
DO_NOT_VALIDATE=false

# Bin directory for installation
BIN="/usr/local/bin"

# Github URLs
GITHUB_API_URL_RELEASES="https://api.github.com/repos/bufbuild/buf/releases"
GITHUB_API_URL_LATEST="${GITHUB_API_URL_RELEASES}/latest"

# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--version=*)
VERSION="${i#*=}"
shift
;;
VERSION="${1#*=}"
;;
--do-not-validate)
DO_NOT_VALIDATE=true
;;
--list)
LIST_VERSIONS=true
shift
;;
LIST_ONLY=true
;;
--help)
SHOW_HELP=true
;;
*)
# unknown option
;;
esac
echo "Unknown option: $1" >&2
exit 1
;;
esac
shift
done

# Logic for listing versions
if [ "$LIST_VERSIONS" = true ]; then
list_versions
exit 0
if [[ "$SHOW_HELP" == true ]]; then
show_help
exit 0
fi

# Set the default version to the latest if not specified
if [ -z "$VERSION" ]; then
VERSION=$(fetch_latest_version)
validate_dependencies

if [[ "$LIST_ONLY" == true ]] || { [[ -n "$VERSION" ]] && [[ "$DO_NOT_VALIDATE" != true ]]; }; then
AVAILABLE_VERSIONS=$(get_available_versions)
fi

# Installation
echo "Installing buf version $VERSION..."
curl -sSL "https://github.com/bufbuild/buf/releases/download/v${VERSION}/buf-$(uname -s)-$(uname -m)" \
-o "${BIN}/buf" && \
chmod +x "${BIN}/buf"
if [[ "$LIST_ONLY" == true ]]; then
list_versions "$AVAILABLE_VERSIONS"
exit 0
fi

if [[ -z "$VERSION" ]]; then
VERSION=$(curl -sSL "${GITHUB_API_URL_LATEST}" | jq -r '.tag_name')
elif [[ "$DO_NOT_VALIDATE" != true ]]; then
validate_version "$AVAILABLE_VERSIONS"
fi

download_and_install "$VERSION"

0 comments on commit 070d086

Please sign in to comment.