diff --git a/scripts/install-okctl.sh b/scripts/install-okctl.sh new file mode 100644 index 00000000..89e8894d --- /dev/null +++ b/scripts/install-okctl.sh @@ -0,0 +1,122 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +# Default values +GITHUB_REPO="oceanbase/ob-operator" +GITHUB_HOST="https://github.com" +VERSION="0.1.0" +OS=$(uname -s | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m) +BINARY_NAME="okctl" + +# Map architecture names +case ${ARCH} in + x86_64|amd64) + ARCH=amd64 + ;; + aarch64|arm64) + ARCH=arm64 + ;; + *) + echo "Unsupported architecture: ${ARCH}" + exit 1 + ;; +esac + +# Help message +usage() { + cat </dev/null 2>&1; then + missing_tools+=("curl") + fi + if ! command -v tar >/dev/null 2>&1; then + missing_tools+=("tar") + fi + + if [ ${#missing_tools[@]} -ne 0 ]; then + echo "Error: Required tools are missing: ${missing_tools[*]}" + echo "Please install them and try again" + exit 1 + fi +} + +# Download and install the binary +install_binary() { + local version=$1 + local BINARY_NAME="okctl" + local download_url="${GITHUB_HOST}/${GITHUB_REPO}/releases/download/cli-${version}/${BINARY_NAME}_${version}_${OS}_${ARCH}.tar.gz" + + echo "Downloading ${BINARY_NAME} ${version}..." + if ! curl -L -o "${BINARY_NAME}_${version}.tar.gz" "${download_url}"; then + echo "Error: Failed to download ${download_url}" + exit 1 + fi + + echo "Extracting to current directory..." + if ! tar -xzf "${BINARY_NAME}_${version}.tar.gz"; then + echo "Error: Failed to extract archive" + exit 1 + fi + + # Clean up downloaded archive + rm -f "${BINARY_NAME}_${version}.tar.gz" + + echo "Successfully extracted ${BINARY_NAME}-${version} (./${BINARY_NAME}) to current directory" + echo "Execute ./${BINARY_NAME} --help to get started" + + echo "Recommend to move the binary to a directory in your PATH, e.g. /usr/local/bin/ like so:" + echo "sudo mv ./${BINARY_NAME} /usr/local/bin/" +} + +main() { + # Check requirements + check_requirements + + # Install binary + install_binary "${VERSION}" +} + +main +