Skip to content

Instantly share code, notes, and snippets.

@charbonnierg
Last active Oct 8, 2021
Embed
What would you like to do?
Install natscli from bash
#!/usr/bin/env bash
set -eou pipefail
DEFAULT_VERSION="${NATSCLI_VERSION:-0.0.26}"
DEFAULT_ARCH="${ARCH:-amd64}"
# Arch can be specified as first argument or as ARCH env variable
ARCH="${1:-$DEFAULT_ARCH}"
# Version can be specified as second argument or NATSCLI_VERSION env variable
VERSION="${2:-$DEFAULT_VERSION}"
# The name of the release file without the ".zip" prefix
RELEASE="nats-${VERSION}-linux-${ARCH}"
# Construct URL of release file
URI="https://github.com/nats-io/natscli/releases/download/${VERSION}/${RELEASE}.zip"
install_nats() {
# Remove both archive and directory on exit and error
trap "rm -Rf ${RELEASE}*" err exit
# Download using wget (you could use curl or any other CLI http client)
wget -q --show-progress "$URI"
# Extract content
unzip -q "${RELEASE}.zip"
# Ensure nats binary is executable
chmod +x "${RELEASE}/nats"
# You may want to use sudo here
mv "${RELEASE}/nats" /usr/local/bin/
}
display_help() {
echo -e "Usage:"
echo -e ""
echo -e " ./install-natscli.sh [ARCH] [NATSCLI_VERSION]"
echo -e ""
echo -e "* Both arguments can be specified as environment variables. (Example: ARCH=amd64 ./install-natscli.sh)"
echo -e "* Arguments take precedence over environment variables."
echo -e "* Allowed architectures: 'amd64', 'arm64', 'arm7', 'arm6', '386'"
echo -e "* Default natscli version: '${DEFAULT_VERSION}'"
echo -e ""
exit
}
# By default perform install
if [[ -z "${1+x}" ]]; then
install_nats
exit
fi
# If arguments are provided, check if --help or -h flag are present
ARGS=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
display_help
# Exit after displaying help
exit
;;
*)
shift
;;
esac
done
install_nats

How to install

Note: The script requires root permissions as it installs nats executable into /usr/local/bin

curl -sSL https://gist.githubusercontent.com/charbonnierg/b68d85e908c90160a1118838f2cceb63/raw/install-natscli.sh | bash
  • Install specific version:
export NATSCLI_VERSION="0.0.26"
curl -sSL https://gist.githubusercontent.com/charbonnierg/b68d85e908c90160a1118838f2cceb63/raw/install-natscli.sh | bash
  • Install specific arch:
export ARCH="arm7"
curl -sSL https://gist.githubusercontent.com/charbonnierg/b68d85e908c90160a1118838f2cceb63/raw/install-natscli.sh | bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment