Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Last active December 27, 2019 20:35
Show Gist options
  • Save rawiriblundell/f24181fd65f9d1f7559fc828d07cf3f0 to your computer and use it in GitHub Desktop.
Save rawiriblundell/f24181fd65f9d1f7559fc828d07cf3f0 to your computer and use it in GitHub Desktop.
Setup the glow markdown viewer
#!/bin/bash
# Functionalise 'command -v' to allow 'if get_command [command]' idiom
get_command() { command -v "${1}" &>/dev/null; }
# TO-DO: Update environment to ensure a sane $ARCH variable
# This will allow us to make this portable to various arm implementations
setup_glow() {
# If it's already present, there's nothing to do here
if get_command glow; then
printf -- '%s\n' "'glow' already exists in PATH"
return 0
fi
# Otherwise, check for our required commands
for command in curl jq; do
if ! get_command "${command}"; then
printf -- '%s\n' "setup_glow(): '${command}' required but not found in PATH"
return 1
fi
done
case "$(uname -s)" in
(Darwin*)
# OSX without 'brew'? I don't need that kind of negativity in my life.
if ! get_command brew; then
printf -- '%s\n' "setup_glow(): 'brew' is required but not found in PATH"
return 1
else
brew install charmbracelet/homebrew-tap/glow
fi
;;
("Linux"|"linux-gnu"|"GNU"*)
# Setup this helper function
get_glow_target() {
curl -s https://api.github.com/repos/charmbracelet/glow/releases/latest |
jq -r --arg url_test "${1:?No target specified}" '
.assets[]
| select(.browser_download_url | test($url_test))
| .browser_download_url'
}
(
cd /tmp || return 1
if get_command dpkg; then
target_url="$(get_glow_target linux_amd64.deb)"
printf -- '%s\n' "Downloading ${target_url}..."
curl -sLO "${target_url}"
sudo dpkg -i glow_*_linux_amd64.deb
elif get_command rpm; then
target_url="$(get_glow_target linux_amd64.rpm)"
printf -- '%s\n' "Downloading ${target_url}..."
curl -sLO "${target_url}"
sudo rpm -ivh glow_*_linux_amd64.rpm
else
target_url="$(get_glow_target linux_x86_64.tar.gz)"
printf -- '%s\n' "Downloading ${target_url}..."
curl -sLO "${target_url}"
tar xzf glow_*_x86_64.tar.gz glow
sudo install -g root -o root -m 755 glow /usr/local/bin
rm -f glow
fi
rm -f glow_*_linux_*
)
;;
esac
}
setup_glow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment