#!/bin/bash # # Copyright (c) 2020 Bubble, Inc. All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/ # # Utility functions and standard pre-processing for many bubble CLI commands. # function die() { if [[ -z "${SCRIPT}" ]]; then echo 1>&2 "${1}" else echo 1>&2 "${SCRIPT}: ${1}" fi exit 1 } function handle_help_request() { if [[ -z "${2}" ]]; then return fi if [[ ${2} == "-h" || ${2} == "--help" ]]; then while IFS='' read -r line || [[ -n "$line" ]]; do if [[ ${line} =~ ^#.* ]]; then if [[ ! ${line} =~ ^#!/bin/bash.* ]]; then echo "${line}" fi else break fi done <"${1}" exit 1 fi } # Validate that argument is "hostname" or "user@hostname" # Validate that user (if present) is valid username and hostname not invalid # Does not perform a full RFC-compliant check, just some basics function validate_user_at_host() { HOST="${1}" if [[ -z "$(echo "${HOST}" | grep '@')" ]] ; then if [[ $(echo "${HOST}" | tr -cd '[:alnum:].-' | wc -c) -ne ${#HOST} ]] ; then die "Invalid host: ${HOST}" fi else USER_PART="$(echo "${HOST}" | awk -F '@' '{print $1}')" HOST_PART="$(echo "${HOST}" | awk -F '@' '{print $2}')" if [[ $(echo "${USER_PART}" | tr -cd '[:alnum:].-' | wc -c) -ne ${#USER_PART} ]] ; then die "Invalid user: ${USER_PART}" elif [[ $(echo "${HOST_PART}" | tr -cd '[:alnum:].-' | wc -c) -ne ${#HOST_PART} ]] ; then die "Invalid host: ${HOST_PART}" fi fi } function make_temp() { prefix="${1}" suffix="${2}" echo "$(mktemp ${prefix}.${suffix}.XXXXXXXX)" } function make_temp_dir() { prefix="${1}" suffix="${2}" echo "$(mktemp -d ${prefix}.${suffix}.XXXXXXXX)" } function quote_args() { args="" for i in "$@"; do if [[ "$i" =~ \ |\' ]]; then i="${i//\\/\\\\}" args="$args \"${i//\"/\\\"}\"" else args="$args ${i}" fi done echo -n ${args} } handle_help_request "${0}" "${1}" # Ensure we can find run.sh if [[ -z "${BUBBLE_SCRIPTS}" ]] ; then BC_DIR="$(cd "$(dirname "${0}")" && pwd)" RUN_SH="$(find "${BC_DIR}" -type f -name "run.sh" | head -1)" if [[ -z "${RUN_SH}" ]] ; then RUN_SH="$(find . -type f -name "run.sh" | head -1)" fi if [[ -z "${RUN_SH}" ]] ; then RUN_SH="$(find ${BC_DIR}/../../bubble -type f -name "run.sh" | head -1)" fi if [[ -z "${RUN_SH}" ]] ; then die "run.sh script not found. Set BUBBLE_SCRIPTS to be the directory containing run.sh" fi BUBBLE_SCRIPTS="$(dirname "${RUN_SH}")" elif [[ ! -f "${BUBBLE_SCRIPTS}/run.sh" ]] ; then die "run.sh script not found in BUBBLE_SCRIPTS dir (${BUBBLE_SCRIPTS})" fi if [[ -z "${BUBBLE_JAR}" ]] ; then if [[ -f "${HOME}/api/bubble.jar" ]] ; then BUBBLE_JAR="${HOME}/api/bubble.jar" elif [[ -f "/home/bubble/api/bubble.jar" ]] ; then BUBBLE_JAR="/home/bubble/api/bubble.jar" elif [[ -f "${BUBBLE_SCRIPTS}/../bubble.jar" ]] ; then BUBBLE_JAR="${BUBBLE_SCRIPTS}/../bubble.jar" else BUBBLE_JAR="$(find "${BUBBLE_SCRIPTS}/../bubble-server/target" -type f -name "bubble*.jar" | head -1)" if [[ -n "${BUBBLE_JAR}" ]] ; then # Use full jar if available. Client libraries like to have the JS engine for example. This is stripped from the server. BUBBLE_FULL_JAR="$(find "${BUBBLE_SCRIPTS}/../bubble-server/target" -type f -name "bubble*-full.jar" | head -1)" if [[ -n "${BUBBLE_FULL_JAR}" ]] ; then BUBBLE_JAR="${BUBBLE_FULL_JAR}" fi fi fi fi if [[ -z "${BUBBLE_JAR}" ]] ; then echo 1>&2 "warning: bubble jar could not be located" fi # Check to see if we are on the PATH, if not suggest that we could be BUBBLE_BIN="$(cd "$(dirname "${0}")" && pwd)" if [[ -z "${BUBBLE_SKIP_PATH_WARNING}" && -z "$(which "$(basename "${0}")")" ]] ; then echo 1>&2 "Note: ${BUBBLE_BIN} is not on your PATH. To make things easier, add it to your PATH:" echo 1>&2 "" echo 1>&2 "export PATH=\${PATH}:${BUBBLE_BIN}" echo 1>&2 "" echo 1>&2 "(set BUBBLE_SKIP_PATH_WARNING to silence this warning)" export BUBBLE_SKIP_PATH_WARNING=1 fi