1+ #! /bin/bash
2+ # SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
3+ # SPDX-License-Identifier: Apache-2.0
4+
5+ # This script installs the ToolHive CLI (thv) in a portable way that works
6+ # across different environments (dev containers, CI/CD, Vercel, etc.).
7+
8+ set -euo pipefail
9+
10+ # Check if jq is installed
11+ if ! command -v jq > /dev/null 2>&1 ; then
12+ echo " Error: 'jq' is required but not installed. Please install jq and try again."
13+ exit 1
14+ fi
15+
16+ API_ENDPOINT=" https://api.github.com/repos/stacklok/toolhive/releases/latest"
17+
18+ # Fetch release information
19+ RELEASE_JSON=$( curl -sf " $API_ENDPOINT " || {
20+ echo " Failed to fetch release information from GitHub API"
21+ exit 1
22+ })
23+ RELEASE_VERSION=$( echo " $RELEASE_JSON " | jq -r ' .tag_name // empty' | sed ' s/^v//' )
24+ RELEASE_TARBALL=$( echo " $RELEASE_JSON " | jq -r \
25+ --arg version " $RELEASE_VERSION " \
26+ ' .assets[] | select(.name == "toolhive_" + $version + "_linux_amd64.tar.gz") | .browser_download_url // empty' )
27+
28+ if [ -z " $RELEASE_TARBALL " ]; then
29+ echo " Failed to get release tarball URL for release: ${RELEASE_VERSION} "
30+ echo " Please check if the tag exists in the repository"
31+ exit 1
32+ fi
33+
34+ # Determine installation location based on write permissions
35+ if [[ -w " /usr/local/bin" ]]; then
36+ # Can write to /usr/local/bin (e.g., CI/CD environments like Vercel)
37+ INSTALL_DIR=" /usr/local/bin"
38+ echo " Installing to /usr/local/bin"
39+ else
40+ # Use user-local directory (e.g., dev containers)
41+ INSTALL_DIR=" $HOME /.local/bin"
42+ echo " Installing to ~/.local/bin"
43+
44+ # Create user bin directory if it doesn't exist
45+ mkdir -p " $INSTALL_DIR "
46+
47+ # Automatically add ~/.local/bin to PATH in shell profile files
48+ for profile in ~ /.bashrc ~ /.zshrc ~ /.profile; do
49+ if [[ -f " $profile " ]]; then
50+ # Check if the PATH export already exists to avoid duplicates
51+ if ! grep -q ' export PATH="$HOME/.local/bin:$PATH"' " $profile " 2> /dev/null; then
52+ echo ' export PATH="$HOME/.local/bin:$PATH"' >> " $profile "
53+ echo " Added ~/.local/bin to PATH in $profile "
54+ fi
55+ fi
56+ done
57+
58+ # Also set PATH for current session
59+ export PATH=" $HOME /.local/bin:$PATH "
60+ fi
61+
62+ # Download the release tarball and extract the binary
63+ echo " Downloading ToolHive CLI release $RELEASE_VERSION "
64+ curl -s -L " $RELEASE_TARBALL " -o /tmp/toolhive.tar.gz
65+ tar -xzf /tmp/toolhive.tar.gz -C /tmp thv
66+ chmod +x /tmp/thv
67+ cp /tmp/thv " $INSTALL_DIR /thv"
68+ rm -f /tmp/toolhive.tar.gz /tmp/thv
69+
70+ echo " ToolHive CLI (thv) installed successfully. Version: $RELEASE_VERSION "
71+ echo " The 'thv' command is now available in your PATH."
0 commit comments