#!/bin/sh set -e RESET="\\033[0m" RED="\\033[31;1m" GREEN="\\033[32;1m" YELLOW="\\033[33;1m" BLUE="\\033[34;1m" WHITE="\\033[37;1m" OPEN_ISSUE_URL="https://github.com/buildnote/buildnote-issues/issues/new/choose" BUILDNOTE_GITHUB_DOWNLOAD_BASE_URL="https://github.com/buildnote/releases/releases/download" BUILDNOTE_DOWNLOAD_BASE_URL="https://get.buildnote.io/releases/cli" print_unsupported_platform() { >&2 say_red "error: We're sorry, but it looks like Buildnote is not supported on your platform" >&2 say_red " We support 64-bit versions of Linux, macOS and are interested in supporting" >&2 say_red " more platforms. Please open an issue at ${OPEN_ISSUE_URL} and" >&2 say_red " let us know what platform you're using!" } say_green() { [ -z "${SILENT}" ] && printf "%b%s%b\\n" "${GREEN}" "$1" "${RESET}" return 0 } say_red() { printf "%b%s%b\\n" "${RED}" "$1" "${RESET}" } say_yellow() { [ -z "${SILENT}" ] && printf "%b%s%b\\n" "${YELLOW}" "$1" "${RESET}" return 0 } say_blue() { [ -z "${SILENT}" ] && printf "%b%s%b\\n" "${BLUE}" "$1" "${RESET}" return 0 } say_white() { [ -z "${SILENT}" ] && printf "%b%s%b\\n" "${WHITE}" "$1" "${RESET}" return 0 } at_exit() { # shellcheck disable=SC2181 # https://github.com/koalaman/shellcheck/wiki/SC2181 # Disable because we don't actually know the command we're running STATUS="$?" if [ "$STATUS" -ne 0 ] && [ "$STATUS" -ne 33 ]; then >&2 say_red >&2 say_red "We're sorry, but it looks like something might have gone wrong during installation." >&2 say_red "If you need help, please raise an issue on ${OPEN_ISSUE_URL}" fi } trap at_exit EXIT VERSION="latest" INSTALL_ROOT="" NO_EDIT_PATH="" SILENT="" FORCE="false" while [ $# -gt 0 ]; do case "$1" in --version) if [ "$2" != "latest" ]; then VERSION=$2 fi ;; --silent) SILENT="--silent" ;; --force) FORCE="true" ;; --install-root) INSTALL_ROOT=$2 ;; --no-edit-path) NO_EDIT_PATH="true" ;; esac shift done BUILDNOTE_GITHUB_LATEST_VERSION_URL="${BUILDNOTE_GITHUB_DOWNLOAD_BASE_URL}/buildnote-cli-latest/latest_version" BUILDNOTE_LATEST_VERSION_URL="${BUILDNOTE_DOWNLOAD_BASE_URL}/latest_version" if [ "$VERSION" = "latest" ]; then if ! VERSION=$(curl --retry 3 --fail --silent -L "${BUILDNOTE_GITHUB_LATEST_VERSION_URL}"); then >&2 say_red "error: could not determine latest version of Buildnote from ${BUILDNOTE_GITHUB_LATEST_VERSION_URL}" >&2 say_red " check your internet and try again; if the problem persists, file an" >&2 say_red " issue at ${OPEN_ISSUE_URL}" VERSION="latest" if ! VERSION=$(curl --retry 3 --fail --silent -L "${BUILDNOTE_LATEST_VERSION_URL}"); then >&2 say_red "error: could not determine latest version of Buildnote from ${BUILDNOTE_LATEST_VERSION_URL}" >&2 say_red " check your internet and try again; if the problem persists, file an" >&2 say_red " issue at ${OPEN_ISSUE_URL}" VERSION="latest" fi fi fi OS="" case $(uname) in "Linux") OS="linux";; "Darwin") OS="darwin";; *) print_unsupported_platform exit 1 ;; esac ARCH="" case $(uname -m) in "x86_64") ARCH="x64";; "arm64") ARCH="arm64";; "aarch64") ARCH="arm64";; *) print_unsupported_platform exit 1 ;; esac BUILDNOTE_GITHUB_DOWNLOAD_URL="${BUILDNOTE_GITHUB_DOWNLOAD_BASE_URL}/buildnote-cli-${VERSION}/buildnote-${VERSION}-${OS}-${ARCH}" BUILDNOTE_DOWNLOAD_URL="${BUILDNOTE_DOWNLOAD_BASE_URL}/buildnote-${VERSION}-${OS}-${ARCH}" BUILDNOTE_INSTALL_ROOT=${INSTALL_ROOT} if [ "$BUILDNOTE_INSTALL_ROOT" = "" ]; then BUILDNOTE_INSTALL_ROOT="${HOME}/.buildnote" fi BUILDNOTE_CLI="${BUILDNOTE_INSTALL_ROOT}/buildnote" if [ -d "${BUILDNOTE_CLI}" ]; then say_red "error: ${BUILDNOTE_CLI} already exists and is a directory, refusing to proceed." exit 1 elif [ ! -f "${BUILDNOTE_CLI}" ]; then say_blue "=== Installing Buildnote v.${VERSION} ===" else if [ "$(${BUILDNOTE_CLI} version)" != "${VERSION}" ]; then say_blue "=== Updating Buildnote from v.$(${BUILDNOTE_CLI} version) to v.${VERSION} ===" else if [ "${FORCE}" != "true" ]; then say_blue "=== Buildnote v.${VERSION} is already installed ===" exit 0 else say_blue "=== Buildnote v.${VERSION} is already installed but forcing reinstall ===" fi fi fi mkdir -p "${BUILDNOTE_INSTALL_ROOT}" say_white "+ Downloading ${BUILDNOTE_GITHUB_DOWNLOAD_URL} to ${BUILDNOTE_CLI}..." if ! curl --retry 2 --fail ${SILENT} -L -o "${BUILDNOTE_CLI}" "${BUILDNOTE_GITHUB_DOWNLOAD_URL}"; then >&2 say_red "error: failed to download ${BUILDNOTE_GITHUB_DOWNLOAD_URL}" >&2 say_red " check your internet and try again; if the problem persists, file an" >&2 say_red " issue at ${OPEN_ISSUE_URL}" say_white "+ Downloading ${BUILDNOTE_DOWNLOAD_URL} to ${BUILDNOTE_CLI}..." if ! curl --retry 2 --fail ${SILENT} -L -o "${BUILDNOTE_CLI}" "${BUILDNOTE_DOWNLOAD_URL}"; then >&2 say_red "error: failed to download ${BUILDNOTE_DOWNLOAD_URL}" >&2 say_red " check your internet and try again; if the problem persists, file an" >&2 say_red " issue at ${OPEN_ISSUE_URL}" exit 1 fi fi # Now that we have installed Buildnote, if it is not already on the path, let's add a line to the # user's profile to add the folder to the PATH for future sessions. if [ "${NO_EDIT_PATH}" != "true" ] && ! command -v buildnote >/dev/null; then # If we can, we'll add a line to the user's .profile adding ${BUILDNOTE_INSTALL_ROOT} to the PATH SHELL_NAME=$(basename "${SHELL}") PROFILE_FILE="" case "${SHELL_NAME}" in "bash") # Terminal.app on macOS prefers .bash_profile to .bashrc, so we prefer that # file when trying to put our export into a profile. On *NIX, .bashrc is # preferred as it is sourced for new interactive shells. if [ "$(uname)" != "Darwin" ]; then if [ -e "${HOME}/.bashrc" ]; then PROFILE_FILE="${HOME}/.bashrc" elif [ -e "${HOME}/.bash_profile" ]; then PROFILE_FILE="${HOME}/.bash_profile" fi else if [ -e "${HOME}/.bash_profile" ]; then PROFILE_FILE="${HOME}/.bash_profile" elif [ -e "${HOME}/.bashrc" ]; then PROFILE_FILE="${HOME}/.bashrc" fi fi ;; "zsh") if [ -e "${ZDOTDIR:-$HOME}/.zshrc" ]; then PROFILE_FILE="${ZDOTDIR:-$HOME}/.zshrc" fi ;; esac if [ -n "${PROFILE_FILE}" ]; then LINE_TO_ADD="export PATH=\$PATH:${BUILDNOTE_INSTALL_ROOT}" if ! grep -q "# add Buildnote to the PATH" "${PROFILE_FILE}"; then say_white "+ Adding ${BUILDNOTE_INSTALL_ROOT} to \$PATH in ${PROFILE_FILE}" printf "\\n# add Buildnote to the PATH\\n%s\\n" "${LINE_TO_ADD}" >> "${PROFILE_FILE}" fi EXTRA_INSTALL_STEP="+ Please restart your shell or add ${BUILDNOTE_INSTALL_ROOT} to your \$PATH" else EXTRA_INSTALL_STEP="+ Please add ${BUILDNOTE_INSTALL_ROOT} to your \$PATH" fi fi chmod +x "${BUILDNOTE_CLI}" say_blue say_blue "=== Buildnote v.${VERSION} is now installed! ===" if [ "$EXTRA_INSTALL_STEP" != "" ]; then say_white "${EXTRA_INSTALL_STEP}" fi say_green "+ Get started with Buildnote: https://docs.buildnote.io/guide/getting-started"