Last active
February 13, 2019 17:58
-
-
Save rram/419cf6a139cbf6d4c7ddbc855d4e460a to your computer and use it in GitHub Desktop.
Downloads the specified version of a Hashicorp program into your bin directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
PROG=${1?-Program name is required} | |
VERSION=${2?-Program version is required} | |
os=$(uname -s | tr A-Z a-z) | |
# MacOS has a quirk with `arch`: | |
# https://apple.stackexchange.com/questions/140651/why-does-arch-output-i386 | |
case "$(uname -m)" in | |
"x86_64" | "amd64" ) | |
arch="amd64" | |
;; | |
"arm" ) | |
arch="arm" | |
;; | |
* ) | |
arch="i386" | |
;; | |
esac | |
zip="${PROG}_${VERSION}_${os}_${arch}.zip" | |
sha="${PROG}_${VERSION}_SHA256SUMS" | |
sig="${PROG}_${VERSION}_SHA256SUMS.sig" | |
url="https://releases.hashicorp.com/${PROG}/${VERSION}" | |
# Make a place for our work. | |
tempdir=$(mktemp -d) | |
cd $tempdir | |
# Download the binary and signature files. | |
curl -sO "${url}/${zip}" | |
curl -sO "${url}/${sha}" | |
curl -sO "${url}/${sig}" | |
# Verify the signature file is untampered. | |
gpg --verify "${sig}" "${sha}" | |
# Verify the SHASUM matches the zip. | |
grep "${os}_${arch}" "${sha}" | shasum -a 256 -c - | |
# Unzip the binary. | |
unzip -q "${zip}" | |
# Put the binary in to place. | |
mv -f "${PROG}" ~/bin | |
# Clean up. | |
rm -f "${zip}" "${sha}" "${sig}" | |
cd - >/dev/null | |
rmdir $tempdir | |
echo "Upgraded to ${PROG} v${VERSION}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment