diff --git a/README.md b/README.md index ac735138a..cae6cc24a 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,12 @@ Once downloaded, you will have to extract the binary and add it to your PATH variable. For detailed instructions for each of our supported platforms, please visit [installation documentation](https://www.mongodb.com/docs/mongodb-shell/install#mdb-shell-install). +You can also run `download_latest.sh` to download a `mongosh` binary. You can use +this script without cloning the repository thus: +``` +curl -fsSL https://raw.githubusercontent.com/mongodb-js/mongosh/refs/heads/main/download_latest.sh | sh +``` + ## CLI Usage diff --git a/download_latest.sh b/download_latest.sh new file mode 100755 index 000000000..d396f59de --- /dev/null +++ b/download_latest.sh @@ -0,0 +1,96 @@ +#!/bin/sh + +set -o errexit + +MONGOSH_RELEASES_URL=https://downloads.mongodb.com/compass/mongosh.json + +show_download_link() { + echo >&2 "Download mongosh manually from:" + echo >&2 + printf >&2 "\t%s\n", 'https://www.mongodb.com/try/download/shell' +} + +for tool in jq curl; do + which "$tool" >/dev/null || { + echo >&2 "This script requires '$tool'." + exit 1 + } +done + +os=$(uname -o | tr '[:upper:]' '[:lower:]') +arch=$(uname -m) + +case "$os" in + *linux) + ext=tgz + os=linux + ;; + darwin) + ext=zip + ;; + *) + echo >&2 "❌ This script does not support this OS ($os)." + show_download_link + + exit 1 +esac + +# normalize $arch: +case "$arch" in + amd64|x64) + arch=x86_64 + ;; + aarch64) + arch=arm64 + ;; + *) + # Use uname’s reported architecture in the jq query. +esac + +jq_query=$(cat <&2 "❓ No download found for $os on $arch." + show_download_link + exit 1 +fi + +case "$ext" in + zip) + file=$(mktemp) + + echo "Downloading $url to $file …" + trap 'rm -f "$file"' EXIT + + curl -fsSL "$url" > "$file" + echo "Downloaded $ext file; extracting mongosh …" + + unzip -vj "$file" '*/bin/mongosh*' + ;; + tgz) + echo "Downloading & extracting from $url …" + + curl -fsSL "$url" | tar -xzf - \ + --transform "s/.*\///" \ + --wildcards "**/bin/mongosh*" \ + | sed -E 's/^.*[/]//' + + ;; + *) + echo >&2 "Bad file extension: $ext" + show_download_link + exit 1 +esac + +echo "✅ Success! 'mongosh' and its crypto library are now saved in this directory."