Skip to content

Commit 4df5b98

Browse files
committed
add download script
1 parent bf53fba commit 4df5b98

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ Once downloaded, you will have to extract the binary and add it to your PATH
2222
variable. For detailed instructions for each of our supported platforms, please visit
2323
[installation documentation](https://www.mongodb.com/docs/mongodb-shell/install#mdb-shell-install).
2424

25+
You can also run `download_latest.sh` to download a `mongosh` binary. You can use
26+
this script without cloning the repository thus:
27+
```
28+
curl -sSL https://raw.githubusercontent.com/mongodb-js/mongosh/refs/heads/main/download_latest.sh | sh
29+
```
30+
2531
## CLI Usage
2632

2733
<!-- AUTOMATICALLY_INSERT_CLI_USAGE -->

download_latest.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
5+
for tool in jq curl; do
6+
which "$tool" >/dev/null || {
7+
echo >&2 "This script requires '$tool'."
8+
exit 1
9+
}
10+
done
11+
12+
os=$(uname -o | tr '[:upper:]' '[:lower:]')
13+
arch=$(uname -m)
14+
15+
case "$os" in
16+
*linux)
17+
ext=tgz
18+
os=linux
19+
;;
20+
darwin)
21+
ext=zip
22+
;;
23+
*)
24+
echo >&2 "This script does not support this OS ($os). Download mongosh manually."
25+
exit 1
26+
esac
27+
28+
case "$arch" in
29+
amd64|x86_64)
30+
arch=x64
31+
;;
32+
aarch64)
33+
arch=arm64
34+
esac
35+
36+
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)
37+
url=$(printf "%s" "$urls" | grep "\-${os}-${arch}" ||:)
38+
39+
if [ -z "$url" ]; then
40+
cat <<EOL
41+
No download found for $os on $arch; download manually.
42+
43+
URLs considered:
44+
$urls
45+
EOL
46+
exit 1
47+
fi
48+
49+
case "$ext" in
50+
zip)
51+
file=$(mktemp)
52+
53+
echo "Downloading $url to $file"
54+
trap 'rm -f $file' EXIT
55+
56+
curl -fsSL "$url" > "$file"
57+
echo "Downloaded $ext file; extracting mongosh …"
58+
59+
unzip -j "$file" '*/mongosh'
60+
;;
61+
tgz)
62+
echo "Downloading & extracting from $url"
63+
64+
curl -fsSL "$url" | tar -xzf - \
65+
--transform "s/.*\///" \
66+
--wildcards "**/mongosh"
67+
68+
;;
69+
*)
70+
echo >&2 "Bad file extension: $ext"
71+
exit 1
72+
esac
73+
74+
echo "Success! 'mongosh' is now saved in this directory."

0 commit comments

Comments
 (0)