#!/usr/bin/env bash set -e #set -x # Parse command line arguments install_dir="" no_sudo="false" while [[ $# -gt 0 ]]; do case $1 in --install-dir) install_dir="$2" shift 2 ;; --no-sudo) no_sudo="true" shift ;; --help|-h) echo "Usage: $0 [--install-dir DIRECTORY] [--no-sudo]" echo " --install-dir DIRECTORY Install qovery binary to specified directory" echo " --no-sudo Do not use sudo, even if available" echo " --help, -h Show this help message" exit 0 ;; *) echo "Unknown option: $1" echo "Use --help for usage information" exit 1 ;; esac done # Check for required commands for cmd in curl gzip tar; do if ! command -v "$cmd" >/dev/null 2>&1; then echo "[!] Required command '$cmd' is not installed. Please install it and try again." exit 1 fi done echo "##################################" echo "# #" echo "# QOVERY CLI INSTALL #" echo "# #" echo "##################################" echo "" repo="Qovery/qovery-cli" output_tgz="/tmp/qovery.tgz" # Determine install directory and sudo usage if [ -n "$install_dir" ]; then # User specified install directory dest_binary="$install_dir" use_sudo="false" elif [ "$(id -u)" -eq 0 ]; then dest_binary="/usr/local/bin" use_sudo="false" elif [ "$no_sudo" = "true" ]; then # User explicitly disabled sudo dest_binary="." use_sudo="false" elif command -v sudo >/dev/null 2>&1; then dest_binary="/usr/local/bin" use_sudo="true" else dest_binary="." use_sudo="false" fi # Set sudo command variable if [ "$use_sudo" = "true" ]; then sudo="sudo" else sudo="" fi # Create install directory if it doesn't exist if [ ! -d "$dest_binary" ]; then echo "[+] Creating install directory: $dest_binary" $sudo mkdir -p "$dest_binary" fi os=$(uname | tr '[:upper:]' '[:lower:]') case "$(uname -m)" in x86_64 | amd64) arch="amd64" ;; arm64 | aarch64) arch="arm64" ;; *) echo "Un-supported architecture. Please report to us to add it" exit 1 ;; esac find_latest_tag() { # Find through API first echo " [+] Trying through Github API" >&2 response=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" "https://api.github.com/repos/$repo/releases/latest" 2>/dev/null || echo "HTTPSTATUS:000") http_code=$(echo "$response" | grep -o "HTTPSTATUS:[0-9]*" | cut -d: -f2) body=$(echo "$response" | sed -E 's/HTTPSTATUS:[0-9]*$//') if [ "$http_code" -eq 200 ] && [ -n "$body" ]; then tag=$(echo "$body" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' 2>/dev/null) if [ -n "$tag" ] && [ "$tag" != "null" ]; then echo " [+] Successfully retrieved tag from GitHub API: $tag" >&2 echo "$tag" return fi elif [ "$http_code" -eq 403 ]; then echo " [!] GitHub API rate limit reached (HTTP 403)" >&2 elif [ "$http_code" -ne 200 ]; then echo " [!] GitHub API request failed with HTTP code: $http_code" >&2 fi # Find using basic scraping on github repo releases echo " [+] Trying GitHub releases page scraping ..." >&2 tag=$(curl --silent --max-time 15 --location "https://github.com/$repo/releases/latest" 2>/dev/null | \ grep -oE 'href="[^"]*/releases/tag/[^"]*"' | head -1 | \ sed -E 's/.*\/releases\/tag\/([^"]*).*/\1/' 2>/dev/null) if [ -n "$tag" ] && [ "$tag" != "null" ]; then echo " [+] Successfully retrieved tag from GitHub releases page: $tag" >&2 echo "$tag" return fi echo "" } echo "[+] Attempting to get latest available tag..." latest_tag=$(find_latest_tag) if [ -z latest_tag ]; then echo "[!] Impossible to retrieve latest release tag" exit 1 fi # Remove leading 'v' using Bash parameter expansion version="${latest_tag#v}" test -f "$output_tgz" && rm -f "$output_tgz" # First try GitHub echo "[+] Attempting to download Qovery CLI archive from GitHub..." github_url="https://github.com/${repo}/releases/download/${latest_tag}/qovery-cli_${version}_${os}_${arch}.tar.gz" curl -o "$output_tgz" -sL "$github_url" echo "[+] Checking if downloaded file is a valid gzip archive..." if gzip -t "$output_tgz" 2>/dev/null; then success=1 else echo "[!] GitHub download failed or is not a valid gzip archive." # Try mirror URL if GitHub fails echo "[+] Attempting to download Qovery CLI archive from mirror..." mirror_url="https://cli-releases.getqovery.io/releases/latest/qovery-cli_${version}_${os}_${arch}.tar.gz" curl -o "$output_tgz" -sL "$mirror_url" echo "[+] Checking if downloaded file is a valid gzip archive..." if gzip -t "$output_tgz" 2>/dev/null; then success=1 else echo "[!] Mirror download failed or is not a valid gzip archive." success=0 fi fi if [ $success -ne 1 ]; then echo "[!] Failed to download a valid Qovery CLI archive from both GitHub and mirror. Exiting." exit 1 fi echo "[+] Uncompressing qovery binary in $dest_binary directory (sudo permissions may be required)" $sudo tar -xzf "$output_tgz" -C "$dest_binary" qovery rm -f "$output_tgz" echo -e "\nQovery CLI is installed in $dest_binary directory, you can now use 'qovery' command line"