Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -sSL https://raw.githubusercontent.com/mongodb-js/mongosh/refs/heads/main/download_latest.sh | sh
```

## CLI Usage

<!-- AUTOMATICALLY_INSERT_CLI_USAGE -->
Expand Down
74 changes: 74 additions & 0 deletions download_latest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/sh

set -o errexit

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). Download mongosh manually."
exit 1
esac

case "$arch" in
amd64|x86_64)
arch=x64
;;
aarch64)
arch=arm64
esac

urls=$(curl -fsSL https://api.github.com/repos/mongodb-js/mongosh/releases/latest | jq -r '.assets[] | .browser_download_url' | grep -v -e \.sig -e shared -e openssl)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use https://downloads.mongodb.com/compass/mongosh.json for this rather than GH

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely better, yes. Adjusted jq query accordingly.

url=$(printf "%s" "$urls" | grep "\-${os}-${arch}" ||:)

if [ -z "$url" ]; then
cat <<EOL
No download found for $os on $arch; download manually.
URLs considered:
$urls
EOL
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 -j "$file" '*/mongosh'
;;
tgz)
echo "Downloading & extracting from $url"

curl -fsSL "$url" | tar -xzf - \
--transform "s/.*\///" \
--wildcards "**/mongosh"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd probably want mongosh_crypt_v1.so to be extracted next to mongosh

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should maybe know this, but: why so? What breaks if it’s not there? (It worked for me without.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatic encryption for Queryable Encryption & CSFLE don't – not everybody cares about those, but I don't think we'd like to ship "partially broken" versions of mongosh if we don't need to

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, the script now downloads both the executable and the shared library.


;;
*)
echo >&2 "Bad file extension: $ext"
exit 1
esac

echo "Success! 'mongosh' is now saved in this directory."