#!/bin/bash ## Required: exports ## NEXTMV_API_KEY - nextmv api key ## NEXTMV_BASE_URL - the api base to fetch the files ## Optional: exports ## NEXTMV_INSTALL_DEBUG - set to "true" for more verbose output from this script NEXTMV_DIR="${NEXTMV_OVERRIDE_HOME_DIR:-$HOME}/.nextmv" NEXTMV_BASE_URL="${NEXTMV_BASE_URL:-https://api.cloud.nextmv.io}" if [[ "$OSTYPE" == "linux-gnu"* ]]; then OS_NAME="linux" elif [[ "$OSTYPE" == "darwin"* ]]; then OS_NAME="darwin" else echo "Unsupported OS" exit -1 fi if [[ "$(uname -m)" == "x86_64" ]]; then ARCH="amd64" elif [[ "$(uname -m)" == "arm64" || "$(uname -m)" == "aarch64" ]]; then ARCH="arm64" else echo "Unsupported Architecture" exit -1 fi SILENT_FLAG="-s" if [[ $NEXTMV_INSTALL_DEBUG == true ]]; then CURL_VERBOSE="-vvv" TAR_VERBOSE="-vvv" set -x fi echo "Checking latest nextmv cli version..." RESULT=$(curl ${CURL_VERBOSE:-$SILENT_FLAG} -H "Authorization: Bearer $NEXTMV_API_KEY" "$NEXTMV_BASE_URL/v0/internal/tools?file=cli/manifest.yml") ERR=$(echo $RESULT | grep -o '"message":"[^"]*' | grep -o '[^"]*$') if [[ "${ERR}" == "Forbidden" ]]; then echo "API key not authorized" exit -1 fi PS=$(echo $RESULT | grep -o '"url":"[^"]*' | grep -o '[^"]*$') PSU=$(echo "$PS" | sed "s/\\\u0026/\&/g") MANIFEST=$(curl ${CURL_VERBOSE:-$SILENT_FLAG} -L -H "Accept: application/octet-stream" "$PSU") if [[ "$OS_NAME" == "linux" ]]; then VERSION=$(echo $MANIFEST | grep -oP '(?<=currentVersion: v).*') else VERSION=$(echo $MANIFEST | grep -E -o '\d+\.\d+.\d+\S*$') fi if [[ $VERSION == "" ]]; then echo "Unable to retrieve information on the latest version" exit -1 fi if [ ! -d "$NEXTMV_DIR" ]; then mkdir -p $NEXTMV_DIR fi THIRD_PARTY_NOTICES_FILE="third-party-notices.txt" THIRD_PARTY_NOTICES_PATH="cli/v$VERSION/$THIRD_PARTY_NOTICES_FILE" OUTPUT_FILE=$NEXTMV_DIR/$THIRD_PARTY_NOTICES_FILE echo "Getting third party dependency notices..." RESULT=$(curl ${CURL_VERBOSE:-$SILENT_FLAG} -H "Authorization: Bearer $NEXTMV_API_KEY" "$NEXTMV_BASE_URL/v0/internal/tools?file=$THIRD_PARTY_NOTICES_PATH") PS=$(echo $RESULT | grep -o '"url":"[^"]*' | grep -o '[^"]*$') PSU=$(echo "$PS" | sed "s/\\\u0026/\&/g") curl ${CURL_VERBOSE} -L -o "$OUTPUT_FILE" -H "Accept: application/octet-stream" "$PSU" RELEASE_FILE="nextmv_${VERSION}_${OS_NAME}_${ARCH}.tar.gz" RELEASE_PATH="cli/v$VERSION/$RELEASE_FILE" OUTPUT_FILE=$NEXTMV_DIR/$RELEASE_FILE echo "Downloading nextmv cli v$VERSION..." RESULT=$(curl ${CURL_VERBOSE:-$SILENT_FLAG} -H "Authorization: Bearer $NEXTMV_API_KEY" "$NEXTMV_BASE_URL/v0/internal/tools?file=$RELEASE_PATH") PS=$(echo $RESULT | grep -o '"url":"[^"]*' | grep -o '[^"]*$') PSU=$(echo "$PS" | sed "s/\\\u0026/\&/g") curl ${CURL_VERBOSE} -L -o "$OUTPUT_FILE" -H "Accept: application/octet-stream" "$PSU" tar ${TAR_VERBOSE} -xf "$OUTPUT_FILE" -C "$NEXTMV_DIR" rm $OUTPUT_FILE if [[ ! -z $NEXTMV_OVERRIDE_HOME_DIR ]]; then echo "Installation complete. Your PATH has not been updated, you must run nextmv CLI executable from $NEXTMV_DIR" exit 0 fi if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then profile_file="~/.zshrc" shell_profile="$HOME/.zshrc" elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then shell="bash" profile_file="~/.bashrc" shell_profile="$HOME/.bashrc" elif [ -n "$($SHELL -c 'echo $FISH_VERSION')" ]; then shell="fish" if [ -d "$XDG_CONFIG_HOME" ]; then profile_file="$XDG_CONFIG_HOME/fish/config.fish" shell_profile="$XDG_CONFIG_HOME/fish/config.fish" else profile_file="~/.config/fish/config.fish" shell_profile="$HOME/.config/fish/config.fish" fi fi path_updated=false case :$PATH: in *:$NEXTMV_DIR:*) ;; # do nothing *) path_udpated=true if [ "$shell" = "fish" ]; then echo "# Nextmv CLI modifications" >> "$shell_profile" echo "set PATH $NEXTMV_DIR \$PATH" >> "$shell_profile" echo 'set NEXTMV_TOKEN $(type nextmv &> /dev/null && sh -c "tok=\$(nextmv token 2>/dev/null); if [ \$? -eq 0 ]; then echo \$tok; fi")' >> "$shell_profile" else echo "# Nextmv CLI modifications" >> "$shell_profile" echo "export PATH=$NEXTMV_DIR:\$PATH" >> "$shell_profile" echo 'export NEXTMV_TOKEN=$(type nextmv &> /dev/null && sh -c "tok=\$(nextmv token 2>/dev/null); if [ \$? -eq 0 ]; then echo \$tok; fi")' >> "$shell_profile" fi if [ "$shell" = "bash" ]; then profile=$HOME/.profile bash_profile=$HOME/.bash_profile if [ -f $profile ]; then profile_file="~/.profile" echo "# Nextmv CLI modifications" >> "$profile" echo "export PATH=$NEXTMV_DIR:\$PATH" >> "$profile" echo 'export NEXTMV_TOKEN=$(type nextmv &> /dev/null && sh -c "tok=\$(nextmv token 2>/dev/null); if [ \$? -eq 0 ]; then echo \$tok; fi")' >> "$profile" fi if [ -f $bash_profile ]; then profile_file="~/.bash_profile" echo "# Nextmv CLI modifications" >> "$bash_profile" echo "export PATH=$NEXTMV_DIR:\$PATH" >> "$bash_profile" echo 'export NEXTMV_TOKEN=$(type nextmv &> /dev/null && sh -c "tok=\$(nextmv token 2>/dev/null); if [ \$? -eq 0 ]; then echo \$tok; fi")' >> "$bash_profile" fi fi ;; esac echo "Install complete." if [ "$path_udpated" = true ]; then echo "Run \"source $profile_file\" or reload your shell to refresh your shell environment." fi