Last active
April 25, 2018 23:41
-
-
Save assembledadam/81caf7972f79ef4ac9aa8796ac54352e to your computer and use it in GitHub Desktop.
Return name of elasticbeanstalk environment based on keyword
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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