Skip to content

Instantly share code, notes, and snippets.

@assembledadam
Last active April 25, 2018 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save assembledadam/81caf7972f79ef4ac9aa8796ac54352e to your computer and use it in GitHub Desktop.
Save assembledadam/81caf7972f79ef4ac9aa8796ac54352e to your computer and use it in GitHub Desktop.
Return name of elasticbeanstalk environment based on keyword
#!/usr/bin/env bash
REQ_ENV=$1
if [ $# -ne 1 ]
then
echo "Autodetects the name of the desired elasticbeanstalk environment based on a keyword (e.g. staging)."
echo " "
echo " Usage: getenv.sh [staging|prod]"
exit 1
fi
# Return position of given string within another string
#
# @param string $1
# Input string.
# @param string $2
# String that will be searched in input string.
# @param int [$3]
# Offset of an input string.
strpos()
{
local str=${1}
local offset=${3}
if [ -n "${offset}" ]; then
str=`substr "${str}" ${offset}`
else
offset=0
fi
str=${str/${2}*/}
if [ "${#str}" -eq "${#1}" ]; then
echo "false"
return 0
fi
echo $((${#str}+${offset}))
}
ENV_LIST=("$(eb list)")
for ENV in "${ENV_LIST[@]}"
do
RES="$(strpos "${ENV}" "${REQ_ENV}")"
if [ $RES != "false" ]; then
# get rid of star
echo "${ENV//'* '}"
exit 0
fi
done
echo "No environment matching ${REQ_ENV} found."
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment