From 070d08628bd2a4f95c32a73cc5c6aebaf579471d Mon Sep 17 00:00:00 2001 From: Ekrem Seren Date: Mon, 11 Dec 2023 12:08:36 +0300 Subject: [PATCH] Update buf installer script --- scripts/buf-installer.sh | 142 +++++++++++++++++++++++++++++---------- 1 file changed, 108 insertions(+), 34 deletions(-) diff --git a/scripts/buf-installer.sh b/scripts/buf-installer.sh index 50570564..322ba8ca 100755 --- a/scripts/buf-installer.sh +++ b/scripts/buf-installer.sh @@ -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"