#!/bin/bash # Default installation path, can be overridden with environment variable or command-line arg INSTALL_PATH="${INSTALL_PATH:-/usr/local/bin}" # Function to check if a command is installed check_command() { if ! command -v "$1" &> /dev/null; then echo "$1 is not installed. Please install $1 and try again." exit 1 fi } # Function to download and install the K8sGPT CLI install_k8sgpt_cli() { echo "Fetching the latest K8sGPT CLI release..." # Detect the OS type OS_TYPE=$(uname -s) ARCH_TYPE=$(uname -m) # Determine the OS and architecture for the release if [ "$OS_TYPE" == "Darwin" ]; then CLI_OS="Darwin" elif [ "$OS_TYPE" == "Linux" ]; then CLI_OS="Linux" else echo "Unsupported OS. Only macOS and Linux are supported." exit 1 fi # Determine the architecture for macOS or Linux if [ "$ARCH_TYPE" == "x86_64" ]; then CLI_ARCH="x86_64" elif [ "$ARCH_TYPE" == "arm64" ]; then CLI_ARCH="arm64" else echo "Unsupported architecture: $ARCH_TYPE. Only x86_64 and arm64 are supported." exit 1 fi # Fetch the correct release URL based on OS and architecture RELEASE_URL="https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.4.2/k8sgpt_${CLI_OS}_${CLI_ARCH}.tar.gz" # Download the release echo "Downloading K8sGPT CLI from $RELEASE_URL..." curl -L "$RELEASE_URL" -o k8sgpt-cli.tar.gz # Check if the file was downloaded correctly if [ ! -f "k8sgpt-cli.tar.gz" ]; then echo "Failed to download the release. Please check the URL or your network connection." exit 1 fi # Extract the tarball echo "Extracting K8sGPT CLI..." if ! tar -xvzf k8sgpt-cli.tar.gz; then echo "Error extracting the archive. The file might be corrupted." exit 1 fi # Check if the extracted binary exists if [ ! -f "k8sgpt" ]; then echo "The extracted file 'k8sgpt' was not found. There might be an issue with the release." exit 1 fi # Move to specified installation path echo "Installing K8sGPT CLI to $INSTALL_PATH..." sudo mv k8sgpt "$INSTALL_PATH/k8sgpt" sudo chmod +x "$INSTALL_PATH/k8sgpt" # Clean up the tarball rm k8sgpt-cli.tar.gz echo "K8sGPT CLI installed successfully to $INSTALL_PATH." } # Main script logic main() { # Check if curl is installed check_command "curl" # Run the install function install_k8sgpt_cli } # Execute the main function, passing any command-line arguments. main "$@"