#!/bin/bash BUCKET_NAME='xata-cli-versions' CHANNEL=${CHANNEL:-'latest'} VERSION=${VERSION:-''} if [ -z "$VERSION" ]; then URL="https://${BUCKET_NAME}.s3.amazonaws.com/channels/$CHANNEL/manifest.json" else URL="https://${BUCKET_NAME}.s3.amazonaws.com/versions/${VERSION}-${CHANNEL}/manifest.json" fi # Function to extract value from nested JSON using grep and sed json_extract_target() { local json="$1" local target="$2" local field="$3" # Find the exact target section and extract the field value echo "$json" | grep -A 3 "\"$target\": {" | grep "\"$field\":" | sed 's/.*"'$field'": *"\([^"]*\)".*/\1/' | head -1 } echo "Downloading manifest from $URL..." MANIFEST=$(curl -s $URL) if [ -z "$MANIFEST" ]; then echo "Failed to download manifest." exit 1 fi PLATFORM=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) if [ "$ARCH" = "x86_64" ]; then ARCH="x64" fi if [ "$ARCH" = "aarch64" ]; then ARCH="arm64" fi TARGET="${PLATFORM}-${ARCH}" # Check if the existing binary has the same sha256sum as the expected target, if yes, abort the installation EXISTING_BINARY="$HOME/.config/xata/bin/xata" if [ -f "$EXISTING_BINARY" ]; then EXISTING_SHA256SUM=$(shasum -a 256 $EXISTING_BINARY | awk '{ print $1 }') REMOTE_SHA256SUM=$(json_extract_target "$MANIFEST" "$TARGET" "sha256sum") if [ "$EXISTING_SHA256SUM" == "$REMOTE_SHA256SUM" ]; then echo "The existing binary ($EXISTING_BINARY) already has the same SHA256 checksum as the remote binary. No need to download." exit 0 fi fi # Otherwise, download the binary BINARY_URL=$(json_extract_target "$MANIFEST" "$TARGET" "url") if [ -z "$BINARY_URL" ]; then echo "Failed to get binary URL from manifest." exit 1 fi echo "Downloading binary from $BINARY_URL..." curl -# -O $BINARY_URL if [ $? -ne 0 ]; then echo "Failed to download binary." exit 1 fi # Verify the checksum of the downloaded binary SHA256SUM=$(json_extract_target "$MANIFEST" "$TARGET" "sha256sum") if [ -z "$SHA256SUM" ]; then echo "Failed to get SHA256 checksum from manifest." exit 1 fi DOWNLOADED_FILE=$(basename $BINARY_URL) echo "$SHA256SUM $DOWNLOADED_FILE" | shasum -a 256 -c - if [ $? -ne 0 ]; then echo "SHA256 checksum verification failed." exit 1 fi mkdir -p ~/.config/xata/bin mv $DOWNLOADED_FILE ~/.config/xata/bin/xata if [ $? -ne 0 ]; then echo "Failed to move binary to ~/.config/xata/bin." exit 1 fi chmod +x ~/.config/xata/bin/xata echo "Xata CLI was installed successfully." echo "" # Download the pgroll and pgstream binaries in parallel ~/.config/xata/bin/xata roll download & ~/.config/xata/bin/xata stream download & wait echo "Please add the following to your PATH:" echo "export PATH=\"$HOME/.config/xata/bin:\$PATH\""